Binding a DataGrid – or any other data-aware control – to a DataSet or a DataTable at runtime isn’t difficult: you just have to assign the DataTable to the DataGrid’s DataSource property, as in:
' DS is the DataSet variableDataGrid1.DataSource = ds.Tables("Titles")
Alternatively, you can assign the DataSet to the DataSource property and the DataTable’s name to the DataMember property:
DataGrid1.DataSource = dsDataGrid1.DataMember = "Titles"
Binding the DataGrid at design time, however, has a few advantages over doing the binding at runtime, because you can prepare the column layout right in the Windows Forms designer. To do the databinding at design time you must follow two distinct procedures, depending on whether the DataSet comes from database tables or it is a strongly-typed DataSet you’ve defined in Visual Studio.
Here’s the procedure if you want to bind the DataGrid to a DataSet that derives from a database table:
(1) open the Server Explorer window and create a connection to your database if you don’t have one already. You can create a new connection by right-clicking on the DataConnection node or by clicking on the third toolbar icon from the right.
(2) locate the table you’re interested in and drag-and-drop it onto the form that contains the DataGrid that you want to bound it to. This action creates two items in the form’s component tray area: OleDbConnection1 and OleDbDataAdapter1 (or SqlConnection1 and SqlDataAdapter1 if you’re working with the SQL Server .NET Data Provider). You can change their names to make your code more readable.
(3) optionally, right-click on the DataAdapter element and select the Configure Data Adapter menu command to decide whether the Data Adapter should use SQL commands, existing stored procedures, or new stored procedures.
(4) right-click again on the DataAdapter element and select the Generate DataSet command to generate a strongly-typed DataSet class. For example, create a new DataSet class named dsPubs and ensure that the “Add this DataSet to the designer” option is checked. Clicking the OK button will create the class and an instance of it – named dsPubs1 – will appear in the form’s component tray area.
(5) You can now select the DataGrid, then activate the Property window, and assign the DataSet to the DataSource property. Note that you can either assign a specific DataTable or the entire DataSet, but in the latter case you’ll have to assign the DataTable’s name to the DataMember property, exactly as you do when doing the data binding through code.
The procedure is slightly different when your DataSet can’t be generated from database tables. Here’s the procedure to follow in this case:
(1) Select the Project-New Item menu command to create a new DataSet element, and name it accordingly (for example “dsProducts”). This action creates a designer’s surface on which you can design your data.
(2) from the XML Schema tab in the toolbox take an XML element and drop it over the DataSet’s designer. Name the new element after the name of the first DataTable you want to create in the DataSet.
(3) complete the definition of the DataTable by adding fields to the DataTable. The first column is the name of the field, the second column is its type.
(4) optionally, create additional tables and the relations between them.
(5) go back to the form that contains the DataGrid, open the toolbox, and select the Data tab. Select the DataSet element from this tab and drop it to the form’s component tray area: a dialog appears, where you can decide the type of the DataSet being dropped, so you just have to select the dsProducts DataSet in the combobox.
(6) once the typed DataSet is on the form, you can bind it to the DataGrid you can bind it to the DataGrid as you would do with a DataSet created from a database table.