Visual C # Data binding

Source: Internet
Author: User

Data binding for complex components:

In the previous introduction, you learned that data binding to complex components is done by setting certain properties of the component. First, let's introduce the data binding of the ComboBox component.

(1). Data binding for ComboBox components:

After the dataset is obtained, only the three properties of the ComboBox component are set to complete the data binding, and the three properties are: "DisplayMember", "ValueMember". where "DataSource" is the dataset to display, " DisplayMember "is the field that the ComboBox component displays, and" ValueMember "is the actual value used. as follows:

Combobox1.datasource = myDataSet;
Combobox1.displaymember = "PERSON.XM";
Combobox1.valuemember = "PERSON.XM";

Note: This binding is the XM field of the person table in the Access 2000 database. From this you can get the source code (Combo01.cs) of the ComboBox component data binding, this Code operation database is Access 2000:
public class Form1:form
{
Private ComboBox ComboBox1;
Private Button button1;
Private System.Data.DataSet myDataSet;
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
file://Open the data link and get the dataset
GetConnect ();
InitializeComponent ();
}
file://resources used in the cleanup program
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

private void GetConnect ()
{
file://Create a OleDbConnection
String Strcon = "Provider = microsoft.jet.oledb.4.0;" Data Source = Db.mdb ";
OleDbConnection myconn = new OleDbConnection (Strcon);
String strcom = "SELECT * from person";
file://Create a DataSet
myDataSet = new DataSet ();

MyConn.Open ();
file://use OleDbDataAdapter to get a dataset
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
file://bind the dataset to the person datasheet
Mycommand.fill (myDataSet, "person");
file://close this OleDbConnection
Myconn.close ();

}

private void Button1_Click (object sender, System.EventArgs e)
{
Combobox1.datasource = myDataSet;
Combobox1.displaymember = "PERSON.XM";
Combobox1.valuemember = "PERSON.XM";
}
static void Main ()
{
Application.Run (New Form1 ());
}
}

Figure 03: The Program Interface for ComboBox component data binding

Get the ComboBox component to the local database data binding program, it is very convenient to get ComboBox component binding SQL Server 2000 source code (Combox02.cs) specifically as follows:
public class Form1:form
{
Private ComboBox ComboBox1;
Private Button button1;
Private System.Data.DataSet myDataSet;
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
file://Open the data link and get the dataset
GetConnect ();
InitializeComponent ();
}
file://resources used in the cleanup program
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

private void GetConnect ()
{
//Set data connection string, which means opening the SQL Server database, the server name is Server1, the database is data1
String strcon = "Provider = sqloledb.1; Persist security Info = False; User ID = sa; Initial Catalog = data1; Data Source = Server1 ";
OleDbConnection myconn = new OleDbConnection (Strcon);
MyConn.Open ();
String strcom = "SELECT * from person";
file://Creates a dataset
myDataSet = new DataSet (),
file://gets a dataset with OleDbDataAdapter
OleDbDataAdapter myco Mmand = new OleDbDataAdapter (strcom, myconn);
file://bind the dataset to the person datasheet
Mycommand.fill (myDataSet, "person");
file://close this OleDbConnection
Myconn.close ();
}
private void Button1_Click (object sender, System.EventArgs e)
{
Combobox1.datasource = myDataSet;
Combobox1.displaymember = "PERSON.XM";
Combobox1.valuemember = "PERSON.XM";
}
static void Main ()
{
Application.Run (new Form1 ());
}
}

(2). Data binding for the ListBox component:

The data binding of the ListBox component is roughly the same as the data binding of the ComboBox component, but also by setting "DisplayMember", "ValueMember". of which "DataSource" These three properties are completed. And the three attributes represent the same meaning as the ComboBox component in the ListBox component. This allows the ListBox component to data bind to the local database and remote database. Where ListBox01.cs is data binding to the local database , the ListBox02.cs is data-bound to the remote database, as follows:

ListBox01.cs source program code section??
public class Form1:form
{
Private ListBox ListBox1;
Private Button button1;
Private System.Data.DataSet myDataSet;
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
file://Open the data link and get the dataset
GetConnect ();
InitializeComponent ();
}
file://resources used in the cleanup program
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

private void GetConnect ()
{
file://Create a OleDbConnection
String Strcon = "Provider = microsoft.jet.oledb.4.0;" Data Source = Db.mdb ";
OleDbConnection myconn = new OleDbConnection (Strcon);
String strcom = "SELECT * from person";
file://Create a DataSet
myDataSet = new DataSet ();

MyConn.Open ();
file://use OleDbDataAdapter to get a dataset
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
file://bind the dataset to the person datasheet
Mycommand.fill (myDataSet, "person");
file://close this OleDbConnection
Myconn.close ();

}

private void Button1_Click (object sender, System.EventArgs e)
{
Listbox1.datasource = myDataSet;
Listbox1.displaymember = "PERSON.XM";
Listbox1.valuemember = "PERSON.XM";
}
static void Main ()
{
Application.Run (New Form1 ());
}
}

Figure 04: The program interface for data binding to the ListBox component

The following code is the source section of the ListBox component for data binding to a SQL Server 2000 database (↙istbox02.cs):
{
Private ListBox ListBox1;
Private Button button1;
Private System.Data.DataSet myDataSet;
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
file://Open the data link to get the DataSet
GetConnect ();
InitializeComponent ();
}
file://the resource used in the Purge program
protected override void Dispose (bool disposing)
{
if (disposing)
{
if ( Components!= null)
{
Components. Dispose ();
}
}
base. Dispose (disposing);
}

private void GetConnect ()
{
Sets the data connection string, which means that the SQL Server database is opened, the server name is Server1, and the database is Data1
String Strcon = "Provider = sqloledb.1;" Persist security Info = False; User ID = sa; Initial Catalog = data1; Data Source = Server1 ";
OleDbConnection myconn = new OleDbConnection (Strcon);
MyConn.Open ();
String strcom = "SELECT * from person";
file://Create a DataSet
myDataSet = new DataSet ();
file://use OleDbDataAdapter to get a dataset
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
file://bind the dataset to the person datasheet
Mycommand.fill (myDataSet, "person");
file://close this OleDbConnection
Myconn.close ();
}

private void Button1_Click (object sender, System.EventArgs e)
{
Listbox1.datasource = myDataSet;
Listbox1.displaymember = "PERSON.XM";
Listbox1.valuemember = "PERSON.XM";
}
static void Main ()
{
Application.Run (New Form1 ());
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.