I have created Windows Application. In this, I have multiple tables in dataset, now I want to bind that to a single DataGridView. Can anybody help me?
In this blog I am going to describe how to bind the data source with “DataGridView” when window load. Create a Window form application have a look at my previous blog - how to create window form application. Once our window is ready click on Toolbox under “Data” choose “DataGridView” and drag onto the window.
Drag and drop DataGridView control from toolbox to form window. Figure 2. Now choose a data source by right clicking on the DataGridView and then click on Add Project Data Source. We will be adding a new data source to the project right now.
Right-click on the small arrow on the GridView then select Edit Column. The Edit Column window will open, on the bottom-left there is an “ADD” button, click on that. You will see an Add Colum window will open like this. There you must add a field as we do for a bound field in ASP.NET.
DataGridView binding - OLEDB in VB.NETThe DataGridView control can display rows of data from a data source. When you specify a data source for the DataGridView, by default it will construct columns for you automatically. This will be created based on the data types in the data source.
following will show one table of dataset
DataGridView1.AutoGenerateColumns = true; DataGridView1.DataSource = ds; // dataset DataGridView1.DataMember = "TableName"; // table name you need to show
if you want to show multiple tables, you need to create one datatable or custom object collection out of all tables.
if two tables with same table schema
dtAll = dtOne.Copy(); // dtOne = ds.Tables[0] dtAll.Merge(dtTwo); // dtTwo = dtOne = ds.Tables[1] DataGridView1.AutoGenerateColumns = true; DataGridView1.DataSource = dtAll ; // datatable
sample code to mode all tables
DataTable dtAll = ds.Tables[0].Copy(); for (var i = 1; i < ds.Tables.Count; i++) { dtAll.Merge(ds.Tables[i]); } DataGridView1.AutoGenerateColumns = true; DataGridView1.DataSource = dtAll ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With