Bind a DataGrid to a DataSet at design-time

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may