using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
class ExecuteXmlReader
{
public static void Main()
{
SqlConnection mySqlConnection =new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = "SELECT TOP 5 ID, FirstName, LastName " +
"FROM Employee " +
"ORDER BY ID " +
"FOR XML AUTO";
mySqlConnection.Open();
XmlReader myXmlReader = mySqlCommand.ExecuteXmlReader();
myXmlReader.Read();
while (!myXmlReader.EOF) {
Console.WriteLine(myXmlReader.ReadOuterXml());
}
myXmlReader.Close();
mySqlConnection.Close();
}
}
Database ADO.net
How to write and read XML files
using System;
using System.Data;
using System.Data.SqlClient;
class WriteAndReadXML {
public static void Main() {
SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 2 CustomerID, CompanyName, ContactName, " +
"Address " +
"FROM Customers " +
"ORDER BY CustomerID";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
mySqlDataAdapter.Fill(myDataSet, "Customers");
mySqlConnection.Close();
myDataSet.WriteXml("myXmlFile.xml");
myDataSet.WriteXml("myXmlFile2.xml", XmlWriteMode.WriteSchema);
myDataSet.WriteXmlSchema("myXmlSchemaFile.xml");
myDataSet.Clear();
myDataSet.ReadXml("myXmlFile.xml");
DataTable myDataTable = myDataSet.Tables["Customers"];
foreach (DataRow myDataRow in myDataTable.Rows) {
Console.WriteLine("CustomerID = " + myDataRow["CustomerID"]);
Console.WriteLine("CompanyName = " + myDataRow["CompanyName"]);
Console.WriteLine("ContactName = " + myDataRow["ContactName"]);
Console.WriteLine("Address = " + myDataRow["Address"]);
}
}
}
Fill Data from database table to ListView
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdExecute;
private System.Windows.Forms.TextBox txtSql;
private System.Windows.Forms.ListView lvwResult;
private System.ComponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.label1 = new System.Windows.Forms.Label();
this.cmdExecute = new System.Windows.Forms.Button();
this.txtSql = new System.Windows.Forms.TextBox();
this.lvwResult = new System.Windows.Forms.ListView();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = “label1”;
this.label1.Size = new System.Drawing.Size(296, 16);
this.label1.TabIndex = 0;
this.label1.Text = “Enter a SQL query or statement and click Execute.”;
this.cmdExecute.Location = new System.Drawing.Point(227, 224);
this.cmdExecute.Name = “cmdExecute”;
this.cmdExecute.TabIndex = 1;
this.cmdExecute.Text = “Execute”;
this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);
this.txtSql.Location = new System.Drawing.Point(0, 16);
this.txtSql.Multiline = true;
this.txtSql.Name = “txtSql”;
this.txtSql.Size = new System.Drawing.Size(528, 200);
this.txtSql.TabIndex = 2;
this.txtSql.Text = “”;
this.lvwResult.GridLines = true;
this.lvwResult.Location = new System.Drawing.Point(0, 256);
this.lvwResult.Name = “lvwResult”;
this.lvwResult.Size = new System.Drawing.Size(528, 200);
this.lvwResult.TabIndex = 3;
this.lvwResult.View = System.Windows.Forms.View.Details;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 452);
this.Controls.Add(this.lvwResult);
this.Controls.Add(this.txtSql);
this.Controls.Add(this.cmdExecute);
this.Controls.Add(this.label1);
this.Name = “Form1”;
this.Text = “Query Processor”;
this.ResumeLayout(false);
}
static void Main() {
Application.Run(new Form1());
}
private void cmdExecute_Click(object sender, System.EventArgs e) {
SqlConnection conn = new SqlConnection(“server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI”);
try {
lvwResult.Columns.Clear() ;
lvwResult.Items.Clear();
conn.Open();
txtSql.Text =”select * from Employee”;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = txtSql.Text;
SqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; i< dr.FieldCount; i++) { ColumnHeader ch = new ColumnHeader(); ch.Text=dr.GetName(i); lvwResult.Columns.Add(ch); } ListViewItem itmX; while (dr.Read()) { itmX=new ListViewItem(); itmX.Text= dr.GetValue(0).ToString(); for (int i=1 ; i< dr.FieldCount; i++) { itmX.SubItems.Add(dr.GetValue(i).ToString()); } lvwResult.Items.Add(itmX); } dr.Close(); } catch ( System.Data.SqlClient.SqlException ex) { Console.WriteLine("There was an error in executing the SQL." + " Error Message:" + ex.Message, "SQL"); } finally { conn.Close(); } } } [/csharp]
Data Binding 4
/*
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.ComponentModel;
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.ComboBox 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.ComponentModel.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.CompanyName";
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.ComboBox();
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.ComponentModel.ISupportInitialize)(this.grdOrders)).BeginInit();
((System.ComponentModel.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.ComboBoxStyle.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.ComponentModel.ISupportInitialize)(this.grdOrders)).EndInit();
((System.ComponentModel.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());
}
}
}
Data Binding 3
/*
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.ComponentModel;
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.ComponentModel.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.ComponentModel.ISupportInitialize)(this.grdCustomers)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.grdOrders)).BeginInit();
((System.ComponentModel.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.ComponentModel.ISupportInitialize)(this.grdCustomers)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.grdOrders)).EndInit();
((System.ComponentModel.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());
}
}
}
ADO.NET Binding : Unsynchronized
/*
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.ComponentModel;
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.ComponentModel.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());
}
}
}
ADO.NETBinding.zip( 76 k)
ADO.NET Binding : Multiple Control Binding
/*
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.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ADO.NET_Binding
{
///
///
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.ComboBox cboModelName;
///
///
private System.ComponentModel.Container components = null;
public MultipleControlBinding()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// the contents of this method with the code editor.
///
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.ComboBox();
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.ComboBoxStyle.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;
}
}
}
}
ADO.NETBinding.zip( 76 k)[/csharp]