Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind Dataset to DataGridView in windows application

Tags:

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?

like image 734
sonal Avatar asked Jun 19 '12 11:06

sonal


People also ask

How do you bind data to the grid in Windows application?

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.

How can you add DataGridView in your application?

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.

How add DataGridView in Windows form?

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.

What is DataGridView data binding in Ado 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.


1 Answers

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 ; 
like image 186
Damith Avatar answered Sep 30 '22 14:09

Damith