Csharp/CSharp Tutorial/ADO.Net/DataBinding DataGrid — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
Data Binding: DataGrid
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; public class DataGridDataBiningDatabase : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.ListBox listBox1; private System.ruponentModel.Container components = null; public DataGridDataBiningDatabase() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.listBox1 = new System.Windows.Forms.ListBox(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.Location = new System.Drawing.Point(8, 24); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(192, 232); this.dataGrid1.TabIndex = 0; // // listBox1 // this.listBox1.Location = new System.Drawing.Point(216, 32); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(104, 212); this.listBox1.TabIndex = 1; // // DataGridDataBiningDatabase // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(480, 277); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.listBox1, this.dataGrid1}); this.Name = "DataGridDataBiningDatabase"; this.Text = "DataGridDataBiningDatabase"; this.Load += new System.EventHandler(this.DataGridDataBiningDatabase_Load); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DataGridDataBiningDatabase()); } private void FillDataGrid() { string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb"; OleDbConnection conn = new OleDbConnection(ConnectionString); string SQL = "SELECT * FROM Customers"; conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, conn); DataSet ds = new DataSet("Customers"); adapter.Fill(ds, "Customers"); dataGrid1.DataSource = ds.DefaultViewManager; conn.Close(); } private void FillListBox() { string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb"; OleDbConnection conn = new OleDbConnection(ConnectionString); string SQL = "SELECT * FROM Customers"; conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, conn); DataSet ds = new DataSet("Customers"); adapter.Fill(ds, "Customers"); listBox1.DataSource = ds.DefaultViewManager; listBox1.DisplayMember = "CustomerID"; conn.Close(); } private void DataGridDataBiningDatabase_Load(object sender, System.EventArgs e) { FillListBox(); FillDataGrid(); }
}</source>
DataGrid Column Asssignment for data binding
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class DataGridColumnAssignment : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid employeeDataGrid; private System.ruponentModel.Container components = null; public DataGridColumnAssignment() { InitializeComponent(); string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;"; string commandString = "Select ID, FirstName from Employee "; SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet,"Employee"); DataTable dataTable = dataSet.Tables[0]; DataGridTableStyle tableStyle = new DataGridTableStyle(); tableStyle.MappingName = dataTable.TableName; GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles; DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn(); columnStyle.MappingName="ID"; columnStyle.HeaderText = "Employee ID"; columnStyles.Add(columnStyle); columnStyle = new DataGridTextBoxColumn(); columnStyle.MappingName = "FirstName"; columnStyle.HeaderText="Employee First Name"; columnStyles.Add(columnStyle); GridTableStylesCollection tableStyles = employeeDataGrid.TableStyles; tableStyles.Add(tableStyle); employeeDataGrid.DataSource=dataTable; } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.employeeDataGrid = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.employeeDataGrid)).BeginInit(); this.SuspendLayout(); // // employeeDataGrid // this.employeeDataGrid.DataMember = ""; this.employeeDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.employeeDataGrid.Location = new System.Drawing.Point(8, 16); this.employeeDataGrid.Name = "employeeDataGrid"; this.employeeDataGrid.Size = new System.Drawing.Size(448, 176); this.employeeDataGrid.TabIndex = 0; // // DataGridColumnAssignment // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 205); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.employeeDataGrid}); this.Name = "DataGridColumnAssignment"; this.Text = "DataGridColumnAssignment"; ((System.ruponentModel.ISupportInitialize)(this.employeeDataGrid)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DataGridColumnAssignment()); }
}</source>
DataGrid Column Style setting
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class DataGridColumnStyle : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid employeeDataGrid; private System.ruponentModel.Container components = null; public DataGridColumnStyle() { InitializeComponent(); string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;"; string commandString = "Select ID, FirstName from Employee "; SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet,"Employee"); DataTable dataTable = dataSet.Tables[0]; DataGridTableStyle tableStyle = new DataGridTableStyle(); tableStyle.MappingName = dataTable.TableName; GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles; DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn(); columnStyle.MappingName="ID"; columnStyle.HeaderText = "Employee ID"; columnStyles.Add(columnStyle); columnStyle = new DataGridTextBoxColumn(); columnStyle.MappingName = "FirstName"; columnStyle.HeaderText="Employee First Name"; columnStyles.Add(columnStyle); GridTableStylesCollection tableStyles = employeeDataGrid.TableStyles; tableStyles.Add(tableStyle); employeeDataGrid.DataSource=dataTable; } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.employeeDataGrid = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.employeeDataGrid)).BeginInit(); this.SuspendLayout(); // // employeeDataGrid // this.employeeDataGrid.DataMember = ""; this.employeeDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.employeeDataGrid.Location = new System.Drawing.Point(8, 16); this.employeeDataGrid.Name = "employeeDataGrid"; this.employeeDataGrid.Size = new System.Drawing.Size(448, 176); this.employeeDataGrid.TabIndex = 0; // // DataGridColumnStyle // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 205); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.employeeDataGrid}); this.Name = "DataGridColumnStyle"; this.Text = "DataGridColumnStyle"; ((System.ruponentModel.ISupportInitialize)(this.employeeDataGrid)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DataGridColumnStyle()); }
}</source>
DataGrid data binding
<source lang="csharp">using System.Data.SqlClient; using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Diagnostics ;
public class SimpleTest : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; public SimpleTest() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.dataGrid1.CaptionVisible = false; this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 8); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(276, 228); this.dataGrid1.TabIndex = 0; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(292, 245); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "Form1"; this.Text = "Simple Data Binding"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new SimpleTest()); }
private void Form1_Load(object sender, System.EventArgs e) { string connectionString = @"Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI"; string SQL = "SELECT * FROM Customers"; SqlConnection con = new SqlConnection(connectionString); SqlCommand com = new SqlCommand(SQL, con); SqlDataAdapter adapter = new SqlDataAdapter(com); DataSet ds = new DataSet("Northwind"); con.Open(); adapter.FillSchema(ds,SchemaType.Mapped , "Customers"); adapter.Fill(ds, "Customers"); com.rumandText = "SELECT * FROM Products"; adapter.FillSchema(ds,SchemaType.Mapped , "Products"); adapter.Fill(ds, "Products"); com.rumandText = "SELECT * FROM Suppliers"; adapter.FillSchema(ds,SchemaType.Mapped , "Suppliers"); adapter.Fill(ds, "Suppliers"); con.Close(); dataGrid1.DataSource=ds; } }</source>
DataGrid DataSource Setting
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class DataGridDataSourceSetting : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid dgBugs; private System.ruponentModel.Container components = null; public DataGridDataSourceSetting() { InitializeComponent(); string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;"; string commandString = "Select * from Employee"; SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet,"Employee"); DataTable dataTable = dataSet.Tables[0]; dgBugs.DataSource=dataTable; } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.dgBugs = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.dgBugs)).BeginInit(); this.SuspendLayout(); // // dgBugs // this.dgBugs.DataMember = ""; this.dgBugs.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dgBugs.Location = new System.Drawing.Point(8, 16); this.dgBugs.Name = "dgBugs"; this.dgBugs.Size = new System.Drawing.Size(448, 176); this.dgBugs.TabIndex = 0; // // DataGridDataSourceSetting // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 205); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dgBugs}); this.Name = "DataGridDataSourceSetting"; this.Text = "DataGridDataSourceSetting"; ((System.ruponentModel.ISupportInitialize)(this.dgBugs)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DataGridDataSourceSetting()); }
}</source>
DataGrid Relation
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class DataGridRelation : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid dataGrid1; private System.ruponentModel.Container components = null; public DataGridRelation() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.ReadOnly = true; this.dataGrid1.Size = new System.Drawing.Size(292, 266); this.dataGrid1.TabIndex = 0; // // DataGridRelation // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1}); this.Name = "DataGridRelation"; this.Text = "DataGridRelation"; this.Load += new System.EventHandler(this.DataGridRelation_Load); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DataGridRelation()); } const string connstr = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;"; private DataSet ds = null; private void DataGridRelation_Load(object sender, System.EventArgs e) { ds = new DataSet(); using ( SqlConnection conn = new SqlConnection( connstr ) ) { conn.Open(); SqlDataAdapter da1 = new SqlDataAdapter( "select * from developers", conn ); da1.Fill( ds, "developers" ); SqlDataAdapter da2 = new SqlDataAdapter( "SELECT empno, name " + "FROM languages", conn ); da2.Fill( ds, "languages" ); ds.Relations.Add( "language", ds.Tables[ "developers" ].Columns[ "empno" ], ds.Tables[ "languages" ].Columns[ "empno" ] ); dataGrid1.DataSource = ds; dataGrid1.DataMember = "developers"; } }
}</source>
Load data from a select statement to DataGrid
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace DataGridExample {
public class DataGridExample : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private System.ruponentModel.Container components = null; public DataGridExample() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(292, 266); this.dataGrid1.TabIndex = 0; // // DataGridExample // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1}); this.Name = "DataGridExample"; this.Text = "DataGrid Example"; this.Load += new System.EventHandler(this.DataGridExample_Load); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new DataGridExample()); } private const string connstr = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;"; private DataSet ds; private void DataGridExample_Load(object sender, System.EventArgs e) { ds = new DataSet(); using ( SqlConnection conn = new SqlConnection( connstr ) ) { conn.Open(); SqlDataAdapter da1 = new SqlDataAdapter( "select * from Employee", conn ); da1.Fill( ds, "Employee" ); dataGrid1.DataSource = ds.Tables[ "Employee" ]; } } }
}</source>