Csharp/C Sharp/Database ADO.net/Data Binding — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:42, 26 мая 2010
Содержание
ADO.NET Binding: Handling Errors
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace ADO.NET_Binding {
/// <summary> /// Summary description for HandlingErrors. /// </summary> public class HandlingErrors : System.Windows.Forms.Form { internal System.Windows.Forms.Label lblErrorSummary; internal System.Windows.Forms.Button cmdNext; internal System.Windows.Forms.Button cmdPrev; internal System.Windows.Forms.Label Label4; internal System.Windows.Forms.GroupBox GroupBox1; internal System.Windows.Forms.Label Label3; internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.TextBox txtModelName; internal System.Windows.Forms.TextBox txtDescription; internal System.Windows.Forms.TextBox txtUnitCost; internal System.Windows.Forms.TextBox txtModelNumber; internal System.Windows.Forms.ruboBox cboModelName; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public HandlingErrors() { // // 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.lblErrorSummary = new System.Windows.Forms.Label(); this.cmdNext = new System.Windows.Forms.Button(); this.cmdPrev = new System.Windows.Forms.Button(); this.Label4 = new System.Windows.Forms.Label(); this.GroupBox1 = new System.Windows.Forms.GroupBox(); this.Label3 = new System.Windows.Forms.Label(); this.Label2 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.txtModelName = new System.Windows.Forms.TextBox(); this.txtDescription = new System.Windows.Forms.TextBox(); this.txtUnitCost = new System.Windows.Forms.TextBox(); this.txtModelNumber = new System.Windows.Forms.TextBox(); this.cboModelName = new System.Windows.Forms.ruboBox(); this.GroupBox1.SuspendLayout(); this.SuspendLayout(); // // lblErrorSummary // this.lblErrorSummary.ForeColor = System.Drawing.Color.Red; this.lblErrorSummary.Location = new System.Drawing.Point(26, 296); this.lblErrorSummary.Name = "lblErrorSummary"; this.lblErrorSummary.Size = new System.Drawing.Size(328, 40); this.lblErrorSummary.TabIndex = 29; // // cmdNext // this.cmdNext.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); this.cmdNext.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdNext.Location = new System.Drawing.Point(322, 348); this.cmdNext.Name = "cmdNext"; this.cmdNext.Size = new System.Drawing.Size(92, 28); this.cmdNext.TabIndex = 28; this.cmdNext.Text = "Next >>"; this.cmdNext.Click += new System.EventHandler(this.cmdNext_Click); // // cmdPrev // this.cmdPrev.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.cmdPrev.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdPrev.Location = new System.Drawing.Point(18, 348); this.cmdPrev.Name = "cmdPrev"; this.cmdPrev.Size = new System.Drawing.Size(92, 28); this.cmdPrev.TabIndex = 27; this.cmdPrev.Text = "<< Prev"; this.cmdPrev.Click += new System.EventHandler(this.cmdPrev_Click); // // Label4 // this.Label4.Location = new System.Drawing.Point(14, 16); this.Label4.Name = "Label4"; this.Label4.Size = new System.Drawing.Size(88, 16); this.Label4.TabIndex = 26; this.Label4.Text = "Select a Record:"; // // GroupBox1 // this.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label3, this.Label2, this.Label1, this.txtModelName, this.txtDescription, this.txtUnitCost, this.txtModelNumber}); this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.GroupBox1.Location = new System.Drawing.Point(14, 48); this.GroupBox1.Name = "GroupBox1"; this.GroupBox1.Size = new System.Drawing.Size(400, 228); this.GroupBox1.TabIndex = 25; this.GroupBox1.TabStop = false; // // Label3 // this.Label3.Location = new System.Drawing.Point(220, 56); this.Label3.Name = "Label3"; this.Label3.Size = new System.Drawing.Size(36, 16); this.Label3.TabIndex = 18; this.Label3.Text = "Cost:"; // // Label2 // this.Label2.Location = new System.Drawing.Point(16, 56); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(52, 16); this.Label2.TabIndex = 17; this.Label2.Text = "Model:"; // // Label1 // this.Label1.Location = new System.Drawing.Point(16, 28); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(52, 16); this.Label1.TabIndex = 16; this.Label1.Text = "Name:"; // // txtModelName // this.txtModelName.Location = new System.Drawing.Point(68, 24); this.txtModelName.Name = "txtModelName"; this.txtModelName.Size = new System.Drawing.Size(316, 21); this.txtModelName.TabIndex = 15; this.txtModelName.Text = ""; // // txtDescription // this.txtDescription.Location = new System.Drawing.Point(12, 92); this.txtDescription.Multiline = true; this.txtDescription.Name = "txtDescription"; this.txtDescription.Size = new System.Drawing.Size(372, 116); this.txtDescription.TabIndex = 14; this.txtDescription.Text = ""; // // txtUnitCost // this.txtUnitCost.Location = new System.Drawing.Point(256, 52); this.txtUnitCost.Name = "txtUnitCost"; this.txtUnitCost.Size = new System.Drawing.Size(128, 21); this.txtUnitCost.TabIndex = 13; this.txtUnitCost.Text = ""; // // txtModelNumber // this.txtModelNumber.Location = new System.Drawing.Point(68, 52); this.txtModelNumber.Name = "txtModelNumber"; this.txtModelNumber.Size = new System.Drawing.Size(136, 21); this.txtModelNumber.TabIndex = 12; this.txtModelNumber.Text = ""; // // cboModelName // this.cboModelName.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList; this.cboModelName.Location = new System.Drawing.Point(106, 10); this.cboModelName.Name = "cboModelName"; this.cboModelName.Size = new System.Drawing.Size(308, 21); this.cboModelName.TabIndex = 24; // // HandlingErrors // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(428, 386); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblErrorSummary, this.cmdNext, this.cmdPrev, this.Label4, this.GroupBox1, this.cboModelName}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "HandlingErrors"; this.Text = "HandlingErrors"; this.Load += new System.EventHandler(this.HandlingErrors_Load); this.GroupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion
[STAThread] static void Main() { Application.Run(new HandlingErrors()); } private BindingManagerBase storeBinding; private int currentPage; private bool errFlag; private void HandlingErrors_Load(object sender, System.EventArgs e) { DataSet dsStore = new DataSet(); dsStore.ReadXmlSchema(Application.StartupPath + "\\store.xsd"); dsStore.ReadXml(Application.StartupPath + "\\store.xml"); cboModelName.DataSource = dsStore.Tables["Products"]; cboModelName.DisplayMember = "ModelName"; txtModelName.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelName"); txtModelNumber.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelNumber"); txtUnitCost.DataBindings.Add("Text", dsStore.Tables["Products"], "UnitCost"); txtDescription.DataBindings.Add("Text", dsStore.Tables["Products"], "Description"); storeBinding = this.BindingContext[dsStore.Tables["Products"]]; storeBinding.PositionChanged += new EventHandler(Binding_PositionChanged); dsStore.Tables["Products"].ColumnChanged += new DataColumnChangeEventHandler(TableChanging); }
private void Binding_PositionChanged(object sender, System.EventArgs e) { if (errFlag) { // Reset the page. storeBinding.Position = currentPage; } else { // Allow the page to change and update the currentPage variable. currentPage = storeBinding.Position; } } private void TableChanging(object sender, System.Data.DataColumnChangeEventArgs e) { string errors = DBStore.ValidateProduct(e.Row); if (errors == "") { errFlag = false; } else { errFlag = true; } lblErrorSummary.Text = errors; } private void cmdNext_Click(object sender, System.EventArgs e) { storeBinding.Position++; } private void cmdPrev_Click(object sender, System.EventArgs e) { storeBinding.Position--; } }
public class DBStore { public static string ValidateProduct(DataRow row) { string errors = ""; if (((decimal)row["UnitCost"]) <= 0) { errors += "* UnitCost value too low\n"; } if (row["ModelNumber"].ToString() == "") { errors += "* You must specify a ModelNumber\n"; } if (row["ModelName"].ToString() == "") { errors += "* You must specify a ModelName\n"; } return errors; } }
}
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/ADO.NETBinding.zip">ADO.NETBinding.zip( 76 k)</a>
ADO.NET Binding: Master Detail
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Data; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; namespace ADO.NET_Binding {
/// <summary> /// Summary description for MasterDetail. /// </summary> public class MasterDetail : System.Windows.Forms.Form { internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.ListBox lstProduct; internal System.Windows.Forms.ListBox lstCategory; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public MasterDetail() { // // 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.Label2 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.lstProduct = new System.Windows.Forms.ListBox(); this.lstCategory = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // Label2 // this.Label2.Location = new System.Drawing.Point(212, 13); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(120, 16); this.Label2.TabIndex = 9; this.Label2.Text = "Products:"; // // Label1 // this.Label1.Location = new System.Drawing.Point(12, 13); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(120, 16); this.Label1.TabIndex = 8; this.Label1.Text = "Categories:"; // // lstProduct // this.lstProduct.Location = new System.Drawing.Point(212, 29); this.lstProduct.Name = "lstProduct"; this.lstProduct.Size = new System.Drawing.Size(208, 225); this.lstProduct.TabIndex = 7; // // lstCategory // this.lstCategory.Location = new System.Drawing.Point(12, 29); this.lstCategory.Name = "lstCategory"; this.lstCategory.Size = new System.Drawing.Size(192, 225); this.lstCategory.TabIndex = 6; // // MasterDetail // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(432, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label2, this.Label1, this.lstProduct, this.lstCategory}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "MasterDetail"; this.Text = "MasterDetail"; this.Load += new System.EventHandler(this.MasterDetail_Load); this.ResumeLayout(false); } #endregion
private BindingManagerBase categoryBinding; private DataSet dsStore = new DataSet();
private void MasterDetail_Load(object sender, System.EventArgs e) { dsStore.ReadXmlSchema(Application.StartupPath + "\\store.xsd"); dsStore.ReadXml(Application.StartupPath + "\\store.xml"); lstCategory.DataSource = dsStore.Tables["Categories"]; lstCategory.DisplayMember = "CategoryName"; lstProduct.DataSource = dsStore.Tables["Products"]; lstProduct.DisplayMember = "ModelName"; categoryBinding = this.BindingContext[dsStore.Tables["Categories"]]; categoryBinding.PositionChanged += new EventHandler(Binding_PositionChanged); // Invoke method once to update child table at startup. Binding_PositionChanged(null,null); } private void Binding_PositionChanged(object sender, System.EventArgs e) { string filter; DataRow selectedRow; // Find the current category row. selectedRow = dsStore.Tables["Categories"].Rows[categoryBinding.Position]; // Create a filter expression using its CategoryID. filter = "CategoryID="" + selectedRow["CategoryID"].ToString() + """; // Modify the view onto the product table. dsStore.Tables["Products"].DefaultView.RowFilter = filter; } [STAThread] static void Main() { Application.Run(new MasterDetail()); } }
}
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/ADO.NETBinding.zip">ADO.NETBinding.zip( 76 k)</a>
ADO.NET Binding : Multiple Control Binding
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace ADO.NET_Binding {
/// <summary> /// Summary description for MultipleControlBinding. /// </summary> public class MultipleControlBinding : System.Windows.Forms.Form { internal System.Windows.Forms.GroupBox GroupBox1; internal System.Windows.Forms.Label lblDescription; internal System.Windows.Forms.Label lblUnitCost; internal System.Windows.Forms.Label lblModelNumber; internal System.Windows.Forms.Button cmdNext; internal System.Windows.Forms.Button cmdPrev; internal System.Windows.Forms.ruboBox cboModelName; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public MultipleControlBinding() { // // 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.GroupBox1 = new System.Windows.Forms.GroupBox(); this.lblDescription = new System.Windows.Forms.Label(); this.lblUnitCost = new System.Windows.Forms.Label(); this.lblModelNumber = new System.Windows.Forms.Label(); this.cmdNext = new System.Windows.Forms.Button(); this.cmdPrev = new System.Windows.Forms.Button(); this.cboModelName = new System.Windows.Forms.ruboBox(); this.GroupBox1.SuspendLayout(); this.SuspendLayout(); // // GroupBox1 // this.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblDescription, this.lblUnitCost, this.lblModelNumber}); this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.GroupBox1.Location = new System.Drawing.Point(18, 47); this.GroupBox1.Name = "GroupBox1"; this.GroupBox1.Size = new System.Drawing.Size(312, 168); this.GroupBox1.TabIndex = 10; this.GroupBox1.TabStop = false; // // lblDescription // this.lblDescription.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.lblDescription.Location = new System.Drawing.Point(8, 58); this.lblDescription.Name = "lblDescription"; this.lblDescription.Size = new System.Drawing.Size(296, 98); this.lblDescription.TabIndex = 6; // // lblUnitCost // this.lblUnitCost.Location = new System.Drawing.Point(168, 16); this.lblUnitCost.Name = "lblUnitCost"; this.lblUnitCost.Size = new System.Drawing.Size(136, 32); this.lblUnitCost.TabIndex = 5; // // lblModelNumber // this.lblModelNumber.Location = new System.Drawing.Point(8, 16); this.lblModelNumber.Name = "lblModelNumber"; this.lblModelNumber.Size = new System.Drawing.Size(140, 32); this.lblModelNumber.TabIndex = 4; // // cmdNext // this.cmdNext.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); this.cmdNext.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdNext.Location = new System.Drawing.Point(238, 227); this.cmdNext.Name = "cmdNext"; this.cmdNext.Size = new System.Drawing.Size(92, 28); this.cmdNext.TabIndex = 9; this.cmdNext.Text = "Next >>"; this.cmdNext.Click += new System.EventHandler(this.cmdNext_Click); // // cmdPrev // this.cmdPrev.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.cmdPrev.Enabled = false; this.cmdPrev.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdPrev.Location = new System.Drawing.Point(18, 227); this.cmdPrev.Name = "cmdPrev"; this.cmdPrev.Size = new System.Drawing.Size(92, 28); this.cmdPrev.TabIndex = 8; this.cmdPrev.Text = "<< Prev"; this.cmdPrev.Click += new System.EventHandler(this.cmdPrev_Click); // // cboModelName // this.cboModelName.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList; this.cboModelName.Location = new System.Drawing.Point(14, 11); this.cboModelName.Name = "cboModelName"; this.cboModelName.Size = new System.Drawing.Size(316, 21); this.cboModelName.TabIndex = 7; // // MultipleControlBinding // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(344, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.GroupBox1, this.cmdNext, this.cmdPrev, this.cboModelName}); this.Name = "MultipleControlBinding"; this.Text = "Manual Navigation Control"; this.Load += new System.EventHandler(this.MultipleControlBinding_Load); this.GroupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion
private BindingManagerBase storeBinding; private void MultipleControlBinding_Load(object sender, System.EventArgs e) { DataSet dsStore = new DataSet(); dsStore.ReadXmlSchema(Application.StartupPath + "\\store.xsd"); dsStore.ReadXml(Application.StartupPath + "\\store.xml"); cboModelName.DataSource = dsStore.Tables["Products"]; cboModelName.DisplayMember = "ModelName"; lblModelNumber.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelNumber"); lblUnitCost.DataBindings.Add("Text", dsStore.Tables["Products"], "UnitCost"); lblDescription.DataBindings.Add("Text", dsStore.Tables["Products"], "Description"); storeBinding = this.BindingContext[dsStore.Tables["Products"]]; storeBinding.PositionChanged += new EventHandler(Binding_PositionChanged); } [STAThread] static void Main() { Application.Run(new MultipleControlBinding()); } private void cmdNext_Click(object sender, System.EventArgs e) { storeBinding.Position++; } private void cmdPrev_Click(object sender, System.EventArgs e) { storeBinding.Position--; } private void Binding_PositionChanged(object sender, System.EventArgs e) { if (storeBinding.Position == storeBinding.Count - 1) { cmdNext.Enabled = false; } else { cmdNext.Enabled = true; } if (storeBinding.Position == 0) { cmdPrev.Enabled = false; } else { cmdPrev.Enabled = true; } } }
}
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/ADO.NETBinding.zip">ADO.NETBinding.zip( 76 k)</a>
ADO.NET Binding : Unsynchronized
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace ADO.NET_Binding {
/// <summary> /// Summary description for Unsynchronized. /// </summary> public class Unsynchronized : System.Windows.Forms.Form { internal System.Windows.Forms.GroupBox grp; internal System.Windows.Forms.ListBox lstProduct; internal System.Windows.Forms.GroupBox grpCategory; internal System.Windows.Forms.ListBox lstCategory; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public Unsynchronized() { // // 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.grp = new System.Windows.Forms.GroupBox(); this.lstProduct = new System.Windows.Forms.ListBox(); this.grpCategory = new System.Windows.Forms.GroupBox(); this.lstCategory = new System.Windows.Forms.ListBox(); this.grp.SuspendLayout(); this.grpCategory.SuspendLayout(); this.SuspendLayout(); // // grp // this.grp.Controls.AddRange(new System.Windows.Forms.Control[] { this.lstProduct}); this.grp.FlatStyle = System.Windows.Forms.FlatStyle.System; this.grp.Location = new System.Drawing.Point(6, 137); this.grp.Name = "grp"; this.grp.Size = new System.Drawing.Size(280, 136); this.grp.TabIndex = 5; this.grp.TabStop = false; // // lstProduct // this.lstProduct.Location = new System.Drawing.Point(16, 24); this.lstProduct.Name = "lstProduct"; this.lstProduct.Size = new System.Drawing.Size(248, 95); this.lstProduct.TabIndex = 1; // // grpCategory // this.grpCategory.Controls.AddRange(new System.Windows.Forms.Control[] { this.lstCategory}); this.grpCategory.FlatStyle = System.Windows.Forms.FlatStyle.System; this.grpCategory.Location = new System.Drawing.Point(6, 0); this.grpCategory.Name = "grpCategory"; this.grpCategory.Size = new System.Drawing.Size(280, 136); this.grpCategory.TabIndex = 4; this.grpCategory.TabStop = false; // // lstCategory // this.lstCategory.Location = new System.Drawing.Point(16, 24); this.lstCategory.Name = "lstCategory"; this.lstCategory.Size = new System.Drawing.Size(248, 95); this.lstCategory.TabIndex = 0; // // Unsynchronized // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(300, 290); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.grp, this.grpCategory}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "Unsynchronized"; this.Text = "Unsynchronized"; this.Load += new System.EventHandler(this.Unsynchronized_Load); this.grp.ResumeLayout(false); this.grpCategory.ResumeLayout(false); this.ResumeLayout(false); } #endregion private void Unsynchronized_Load(object sender, System.EventArgs e) { // Make sure all the controls in this group box have a different binding. grpCategory.BindingContext = new BindingContext(); DataSet dsStore = new DataSet(); dsStore.ReadXmlSchema(Application.StartupPath + "\\store.xsd"); dsStore.ReadXml(Application.StartupPath + "\\store.xml"); lstCategory.DataSource = dsStore.Tables["Categories"]; lstCategory.DisplayMember = "CategoryName"; lstProduct.DataSource = dsStore.Tables["Categories"]; lstProduct.DisplayMember = "CategoryName"; } [STAThread] static void Main() { Application.Run(new Unsynchronized()); } }
}
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/ADO.NETBinding.zip">ADO.NETBinding.zip( 76 k)</a>
Data Binding 3
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace DataBinding_3 {
/// <summary> /// Summary description for DataBinding_3. /// </summary> public class DataBinding_3 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid grdOrders; private System.Windows.Forms.DataGrid grdOrderDetails; private System.Windows.Forms.DataGrid grdCustomers; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public DataBinding_3() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // SqlConnection cn=new SqlConnection(@"data source=(local);uid=sa;password=;database=northwind"); DataSet ds = new DataSet("CustOrders"); SqlDataAdapter daCust=new SqlDataAdapter("select * from customers;select * from orders;select * from [order details]",cn); daCust.Fill(ds); ds.Relations.Add("CustOrder",ds.Tables["Table"].Columns["customerid"],ds.Tables["Table1"].Columns["customerid"]); ds.Relations.Add("OrderDetail",ds.Tables["Table1"].Columns["orderid"],ds.Tables["Table2"].Columns["orderid"]); grdCustomers.DataSource=ds; grdCustomers.DataMember="Table"; grdOrders.DataSource=ds; grdOrders.DataMember="Table.CustOrder"; grdOrderDetails.DataSource=ds; grdOrderDetails.DataMember="Table.CustOrder.OrderDetail"; } /// <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.grdCustomers = new System.Windows.Forms.DataGrid(); this.grdOrders = new System.Windows.Forms.DataGrid(); this.grdOrderDetails = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.grdCustomers)).BeginInit(); ((System.ruponentModel.ISupportInitialize)(this.grdOrders)).BeginInit(); ((System.ruponentModel.ISupportInitialize)(this.grdOrderDetails)).BeginInit(); this.SuspendLayout(); // // grdCustomers // this.grdCustomers.AllowNavigation = false; this.grdCustomers.DataMember = ""; this.grdCustomers.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.grdCustomers.Location = new System.Drawing.Point(40, 16); this.grdCustomers.Name = "grdCustomers"; this.grdCustomers.Size = new System.Drawing.Size(448, 152); this.grdCustomers.TabIndex = 0; // // grdOrders // this.grdOrders.AllowNavigation = false; this.grdOrders.DataMember = ""; this.grdOrders.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.grdOrders.Location = new System.Drawing.Point(40, 176); this.grdOrders.Name = "grdOrders"; this.grdOrders.Size = new System.Drawing.Size(448, 144); this.grdOrders.TabIndex = 1; // // grdOrderDetails // this.grdOrderDetails.DataMember = ""; this.grdOrderDetails.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.grdOrderDetails.Location = new System.Drawing.Point(40, 328); this.grdOrderDetails.Name = "grdOrderDetails"; this.grdOrderDetails.Size = new System.Drawing.Size(448, 136); this.grdOrderDetails.TabIndex = 2; // // DataBinding_3 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(528, 483); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.grdOrderDetails, this.grdOrders, this.grdCustomers}); this.Name = "DataBinding_3"; this.Text = "DataBinding_3"; ((System.ruponentModel.ISupportInitialize)(this.grdCustomers)).EndInit(); ((System.ruponentModel.ISupportInitialize)(this.grdOrders)).EndInit(); ((System.ruponentModel.ISupportInitialize)(this.grdOrderDetails)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new DataBinding_3()); } }
}
</source>
Data Binding 4
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace DataBinding {
/// <summary> /// Summary description for DataBinding4. /// </summary> public class DataBinding4 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid grdOrders; private System.Windows.Forms.DataGrid grdOrderDetails; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.ruboBox cbCust; private System.Windows.Forms.TextBox txtPhoneNo; private System.Windows.Forms.TextBox txtFaxNo; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtContact; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public DataBinding4() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // SqlConnection cn=new SqlConnection(@"data source=(local);uid=sa;password=;database=northwind"); DataSet ds = new DataSet("CustOrders"); SqlDataAdapter daCust=new SqlDataAdapter("select * from customers;select * from orders;select * from [order details]",cn); daCust.Fill(ds); ds.Relations.Add("CustOrder",ds.Tables["Table"].Columns["customerid"],ds.Tables["Table1"].Columns["customerid"]); ds.Relations.Add("OrderDetail",ds.Tables["Table1"].Columns["orderid"],ds.Tables["Table2"].Columns["orderid"]); grdOrders.DataSource=ds; grdOrders.DataMember="Table.CustOrder"; grdOrderDetails.DataSource=ds; grdOrderDetails.DataMember="Table.CustOrder.OrderDetail"; cbCust.DataSource=ds; cbCust.DisplayMember="Table.rupanyName"; txtContact.DataBindings.Add("Text",ds,"Table.ContactName"); txtPhoneNo.DataBindings.Add("Text",ds,"Table.Phone"); txtFaxNo.DataBindings.Add("Text",ds,"Table.Fax"); } /// <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.grdOrders = new System.Windows.Forms.DataGrid(); this.grdOrderDetails = new System.Windows.Forms.DataGrid(); this.cbCust = new System.Windows.Forms.ruboBox(); this.txtPhoneNo = new System.Windows.Forms.TextBox(); this.txtFaxNo = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.txtContact = new System.Windows.Forms.TextBox(); ((System.ruponentModel.ISupportInitialize)(this.grdOrders)).BeginInit(); ((System.ruponentModel.ISupportInitialize)(this.grdOrderDetails)).BeginInit(); this.SuspendLayout(); // // grdOrders // this.grdOrders.AllowNavigation = false; this.grdOrders.DataMember = ""; this.grdOrders.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.grdOrders.Location = new System.Drawing.Point(40, 176); this.grdOrders.Name = "grdOrders"; this.grdOrders.Size = new System.Drawing.Size(448, 144); this.grdOrders.TabIndex = 1; // // grdOrderDetails // this.grdOrderDetails.DataMember = ""; this.grdOrderDetails.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.grdOrderDetails.Location = new System.Drawing.Point(40, 328); this.grdOrderDetails.Name = "grdOrderDetails"; this.grdOrderDetails.Size = new System.Drawing.Size(448, 136); this.grdOrderDetails.TabIndex = 2; // // cbCust // this.cbCust.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList; this.cbCust.Location = new System.Drawing.Point(216, 32); this.cbCust.Name = "cbCust"; this.cbCust.Size = new System.Drawing.Size(240, 21); this.cbCust.TabIndex = 3; // // txtPhoneNo // this.txtPhoneNo.Location = new System.Drawing.Point(216, 96); this.txtPhoneNo.Name = "txtPhoneNo"; this.txtPhoneNo.Size = new System.Drawing.Size(240, 20); this.txtPhoneNo.TabIndex = 4; this.txtPhoneNo.Text = "textBox1"; // // txtFaxNo // this.txtFaxNo.Location = new System.Drawing.Point(216, 128); this.txtFaxNo.Name = "txtFaxNo"; this.txtFaxNo.Size = new System.Drawing.Size(240, 20); this.txtFaxNo.TabIndex = 5; this.txtFaxNo.Text = "textBox2"; // // label1 // this.label1.Location = new System.Drawing.Point(80, 32); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(128, 24); this.label1.TabIndex = 6; this.label1.Text = "Customer Name"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // label2 // this.label2.Location = new System.Drawing.Point(80, 96); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(128, 24); this.label2.TabIndex = 7; this.label2.Text = "Phone Number"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // label3 // this.label3.Location = new System.Drawing.Point(80, 128); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(128, 24); this.label3.TabIndex = 8; this.label3.Text = "Fax Number"; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // label4 // this.label4.Location = new System.Drawing.Point(80, 64); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(128, 24); this.label4.TabIndex = 10; this.label4.Text = "Contact Name"; this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // txtContact // this.txtContact.Location = new System.Drawing.Point(216, 64); this.txtContact.Name = "txtContact"; this.txtContact.Size = new System.Drawing.Size(240, 20); this.txtContact.TabIndex = 9; this.txtContact.Text = "textBox1"; // // DataBinding4 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(520, 483); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label4, this.txtContact, this.label3, this.label2, this.label1, this.txtFaxNo, this.txtPhoneNo, this.cbCust, this.grdOrderDetails, this.grdOrders}); this.Name = "DataBinding4"; this.Text = "DataBinding4"; ((System.ruponentModel.ISupportInitialize)(this.grdOrders)).EndInit(); ((System.ruponentModel.ISupportInitialize)(this.grdOrderDetails)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new DataBinding4()); } }
}
</source>