Microsoft KB Archive/324832

= How To Bind Data to a Control at Design-Time and Convert Data Source Data to Particular .NET-Types Data =

Article ID: 324832

Article Last Modified on 5/10/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q324832



IN THIS TASK
SUMMARY MORE INFORMATION Advanced Data Binding Casting Sample Steps (for a Microsoft Access - Visual C# .NET Windows Program) Create an Access Database for Your Sample Project Create a New Windows Application Project
 * Data Adapter Configuration Wizard

Generate a Data Set Insert a Second Data Set Insert a Button Insert a Data Grid Change the Form Constructor Build the Solution REFERENCES



SUMMARY
This step-by-step article shows you how to fully use Advanced Data Binding in the Visual Studio .NET IDE by using a DataAdapter, a Connection, and two DataSets. This article also shows you how to convert data source data to .NET types/structures-based data, if you have to do so. (Some details are provided for SQL Server and Microsoft Access data types. In particular, this article includes a sample of a Visual C# .NET Windows program with data-binding to Microsoft Access data.)

back to the top



Advanced Data Binding
By using the Advanced Data Binding dialog box, you can bind the properties of some controls to DataTable values at design-time. To open the dialog box, follow these steps:
 * 1) Click to select the control.
 * 2) In Properties, expand (DataBindings).
 * 3) Click Advanced, and then click the ellipsis button (...) to open the Advanced Data Binding dialog box.

When you select design-time data binding, the binding occurs faster because the values are already stored in an in-memory temporary table that is easily accessible by the program. Except for the typical Fill dataAdapter method, you do not have to do any coding, unless you are casting to .NET types data (all of the casting code that you must have is inserted in the Form constructor).

back to the top

Casting
Casting is not a problem if the source data is char (SQL Server) or text (Microsoft Access), because the source data is interpreted by the program directly as a System.String (the same is true for Boolean values and generally numeric values). In this case, advanced DataBinding is performed directly through the IDE at design-time, without coding (except for the typical Fill dataAdapter method).

However, if you must have casting, you can create a second dataset at design-time, that has column fields that are defined by using the IDE. For these columns, which will be filled by particular .NET types data, you must select the System.Object data type. Casting from data source data occurs when the form is initialized (in the Form constructor). For these fields, the data source data must be char (SQL Server) or text (MS Access).

You must have casting if the property is related to particular .NET structure types, for example, System.Drawing.Color (.BackColor, .ForeColor) or System.FlatStyle (.FlatStyle).

The &quot;universal&quot; casting code, from char (SQL Server) or text (MS Access) to the .NET type that you want, is similar to the following lines of sample code, which convert a System.String value to a System.Drawing.Color value: System.Drawing.Color clr; System.String str; clr = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(str); back to the top

Create an Access Database for Your Sample Project
 Create an Access database named Db1.mdb that has a table named DBTable. Define two columns that have the following attributes:

Field Name: PID

Data Type: AutoNumber [Primary Key]

Field Name: BackColor

Data Type: text

  Insert some consistent data, such as the following sample data. Insert only data that is consistent with the System.Drawing.Color structure.     ++---+ |PID | BackColor | ++---+    | 1  | White     | | 2 | Blue      | | 3 | BlueViolet| | 4 | Green     | | 5 | Black     | ++---+                   

back to the top

Create a New Windows Application Project

 * 1) In Visual Studio .NET, on the File menu, point to New, and then click Project.
 * 2) Click Visual C# Projects, and then click Windows Application. Name the program SampleDataBinding. Click OK.
 * 3) Drag an OleDbDataAdapter (named oleDbDataAdapter1) from the Toolbox (under Data)to Form1, and then follow the steps in the Data Adapter Configuration Wizard.

back to the top

 Data Adapter Configuration Wizard To configure oleDbDataAdapter1, follow these steps:  Click New Connection. Use the Data Link Properties dialog box to select the connection to the dtabase that you created earlier. To do this, follow these steps:  On the Provider tab, click to select Microsoft JET 4.0 OLE DB Provider. On the Connection tab, click the ellipsis button, locate and then click the database that you created earlier, click Open, and then click Test Connection. (The connection works correctly if Test connection succeeded appears.)</li> Click OK to close the Data Link Properties dialog box.</li></ol> </li> In the Data Adapter Configuration wizard, click Next.</li> Verify that Use SQL Statement is selected, and then click Next.</li> Use the Query Builder to insert the SQL statement SELECT * FROM DBTable. To do this, follow these steps:  Click Query Builder. In the Add Table dialog box, click to select DBTable, click Add, and then click Close. (If the Add Table dialog box does not appear, right-click in the Query Builder dialog box, and then click Add Table.)</li> In DBTable, click to select the check box for * (All Columns).</li> To verify the connection, right-click the connection in the Table column, and then click Run; the correct Access data appears in the Results pane.</li> Click OK to close the Query Builder dialog box.</li></ol> </li> Click Finish to close the Data Adapter Configuration wizard.</li></ol>

The control oleDbConnection1 is automatically inserted in the project.

back to the top

Generate a Data Set
Generate a data set that is related to oleDbDataAdapter1. To do this, follow these steps:
 * 1) Click to select oleDbDataAdapter1, and then, in Properties, click Generate Dataset.
 * 2) In the Generate Dataset dialog box, click to select New, and then type a name for the dataset. (In the following example, the default name (DataSet1) is used; the object that is added to the designer is named dataSet11.)
 * 3) Verify that the table DBTable (oleDbDataAdapter1) is selected.
 * 4) Verify that the option Add this dataset to the designer is selected.
 * 5) Click OK to close the Generate Dataset dialog box.

back to the top

Insert a Second Data Set
 Drag a second data set from the Toolbox to Form1. The Add Dataset dialog box appears.</li> Click to select Untyped dataset, and then click OK.</li> In Properties, in the DataSetName value text box for the new data set, type dsTemp .</li> Scroll down the Properties list. Next to Tables, click the ellipsis button to open the Tables Collection Editor dialog box.</li> <li>In the Tables Collection Editor dialog box, click Add to add a new member in the dsTemp.Tables collection.</li> <li>In the value text box for the TableName property, type tblTemp .</li> <li>In the value text box for the (Name) property, type datatblTemp.

NOTE: You can have access to the same table by referencing to the DataTable, whose name is defined in the (Name) property; the object dsTemp.Tables[ &quot;tblTemp&quot;] is same as the object datatblTemp.</li> <li>Click Columns and then click the ellipsis button next to (Collection) to open the Columns Collection Editor dialog box.</li> <li>Click Add two times to add two new members in the dsTemp.Tables[&quot;tblTemp&quot;].Columns collection (which is the same as the datatblTemp.Columns collection).</li> <li>The first column corresponds to the first column in db1.DBTable (PID (AutoNumber) column in the Access database). Change the properties of this column to the following:

ColumnName: colPID

DataType: System.Int32

</li> <li>The second column corresponds to the second column in db1.DBTable (BackColor (text) column in the Access database). Change the properties of this column to the following:

ColumnName: colColor

DataType: System.Object

</li> <li>Click Close to close the Columns Collection Editor dialog box.</li> <li>Click Close to close the Tables Collection Editor dialog box.</li></ol>

back to the top

Insert a Button

 * 1) Drag a button from the Toolbox (under Windows Forms) to Form1. By default, the new button is named button1.
 * 2) In Properties, expand (DataBindings) (at the top of the list), click Advanced, and then click the ellipsis button to open the Advanced Data Binding - button1 dialog box.
 * 3) Insert the correct source column for the properties that you want. To do this, follow these steps:
 * 4) Click Text, click the drop-down list arrow, expand datatblTemp, and then double-click colPID. (Casting from numeric fields to System.String fields occurs automatically.)
 * 5) Click BackColor, click the drop-down list arrow, expand datatblTemp, and then double-click colColor.
 * 6) Click Close to close the Advanced Data Binding - button1 dialog box.

back to the top

Insert a Data Grid
Drag a DataGrid (dataGrid1) to the form, and then set the following properties for dataGrid1:

DataSource: dsTemp

DataMember: tblTemp

NOTE: You can also set the DataSource property to datatblTemp, which gives the same result.

back to the top

Change the Form Constructor
If you used dsTemp in your program (instead of datatblTemp), change the Form1 constructor by using the following sample code: public Form1 {  InitializeComponent;

oleDbDataAdapter1.Fill(dataSet11);

DataRow r;  for (int i = 0; i < dataSet11.Tables[&quot;DBTable&quot;].Rows.Count; ++i) {     r = dsTemp.Tables[&quot;tblTemp&quot;].NewRow; dsTemp.Tables[&quot;tblTemp&quot;].Rows.Add(r);

r[&quot;colPID&quot;] = dataSet11.Tables[&quot;DBTable&quot;].Rows[i][&quot;PID&quot;]; r[&quot;colColor&quot;] = (Color)TypeDescriptor.GetConverter(typeof(Color)). ConvertFromString(dataSet11.Tables[&quot;DBTable&quot;].Rows[i][ &quot;BackColor&quot;].ToString); } }  If you used datatblTemp in your program (instead of dsTemp), change the Form1 constructor by using the following sample code: public Form1 {  InitializeComponent;

oleDbDataAdapter1.Fill(dataSet11);

DataRow r;  for (int i = 0; i < dataSet11.Tables[&quot;DBTable&quot;].Rows.Count; ++i) {     r = datatblTemp.NewRow; datatblTemp.Rows.Add(r);

r[&quot;colPID&quot;] = dataSet11.Tables[&quot;DBTable&quot;].Rows[i][&quot;PID&quot;];

r[&quot;colColor&quot;] = (Color)TypeDescriptor.GetConverter(typeof(Color)). ConvertFromString(dataSet11.Tables[&quot;DBTable&quot;].Rows[i][ &quot;BackColor&quot;].ToString); } } back to the top

Build the Solution
Build the solution, and then start the program. When you click each value in dataGrid1, button1 changes its databound properties according to the data that is displayed.

back to the top

<div class="references_section">