Microsoft KB Archive/324832

From BetaArchive Wiki

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

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

MORE INFORMATION

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 "universal" 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

Sample Steps (for a Microsoft Access - Visual C# .NET Windows Program)

Create an Access Database for Your Sample Project

  1. 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

  2. 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:

  1. 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:
    1. On the Provider tab, click to select Microsoft JET 4.0 OLE DB Provider.
    2. 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.)
    3. Click OK to close the Data Link Properties dialog box.
  2. In the Data Adapter Configuration wizard, click Next.
  3. Verify that Use SQL Statement is selected, and then click Next.
  4. Use the Query Builder to insert the SQL statement SELECT * FROM DBTable. To do this, follow these steps:
    1. 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.)
    2. In DBTable, click to select the check box for * (All Columns).
    3. 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.
    4. Click OK to close the Query Builder dialog box.
  5. Click Finish to close the Data Adapter Configuration wizard.

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

  1. Drag a second data set from the Toolbox to Form1. The Add Dataset dialog box appears.
  2. Click to select Untyped dataset, and then click OK.
  3. In Properties, in the DataSetName value text box for the new data set, type dsTemp.
  4. Scroll down the Properties list. Next to Tables, click the ellipsis button to open the Tables Collection Editor dialog box.
  5. In the Tables Collection Editor dialog box, click Add to add a new member in the dsTemp.Tables collection.
  6. In the value text box for the TableName property, type tblTemp.
  7. 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[ "tblTemp"] is same as the object datatblTemp.
  8. Click Columns and then click the ellipsis button next to (Collection) to open the Columns Collection Editor dialog box.
  9. Click Add two times to add two new members in the dsTemp.Tables["tblTemp"].Columns collection (which is the same as the datatblTemp.Columns collection).
  10. 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

  11. 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

  12. Click Close to close the Columns Collection Editor dialog box.
  13. Click Close to close the Tables Collection Editor dialog box.

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:
    1. 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.)
    2. Click BackColor, click the drop-down list arrow, expand datatblTemp, and then double-click colColor.
  4. 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["DBTable"].Rows.Count; ++i)
   {
      r = dsTemp.Tables["tblTemp"].NewRow();
      dsTemp.Tables["tblTemp"].Rows.Add(r);

      r["colPID"] = dataSet11.Tables["DBTable"].Rows[i]["PID"];
 
      r["colColor"] = (Color)TypeDescriptor.GetConverter(typeof(Color)).
ConvertFromString(dataSet11.Tables["DBTable"].Rows[i][ "BackColor"].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["DBTable"].Rows.Count; ++i)
   {
      r = datatblTemp.NewRow();
      datatblTemp.Rows.Add(r);

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

      r["colColor"] = (Color)TypeDescriptor.GetConverter(typeof(Color)).
ConvertFromString(dataSet11.Tables["DBTable"].Rows[i][ "BackColor"].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

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

313482 INFO: Roadmap for Windows Forms Data Binding


313481 INFO: Roadmap for Web Forms Data Binding


For more information, visit the following Microsoft Web Site:

Data Binding with Windows Forms and ADO.NET
http://msdn2.microsoft.com/en-us/library/ms973824.aspx


back to the top

Keywords: kbdatabinding kbhowto kbide kbideproject KB324832