Home > DeveloperSection > Articles > Bind Dataset to DataGridView C#

Bind Dataset to DataGridView C#


C# C# 
Ratings:
1 Comment(s)
 16724  View(s)
Rate this:

Bind Dataset to DataGridView C#

In this article I’m going to explain, how to bind an existing database table items to datagridview.  Here I’ve define some steps which are necessary to perform this task.

Step 1: First of all you’ve to create a windows form if you’re using Visual Studio application then you can simply drag and drop DataGridView control from toolbox which I’ve shown you below.

Bind Dataset to DataGridView C#

If you want to add, edit and delete datagridview column then enable this option in datagridview which I have shown in above image.

Step 2: “Select Data Source” from combobox (red circle in below image).

Bind Dataset to DataGridView C#

Step 3: After selecting data source option select “Add Project Data Source…”

Bind Dataset to DataGridView C#

 

Step 4: Select Database option and click button “Next” as below image.

Bind Dataset to DataGridView C#

Step 5: Now select “Dataset” option and move next step as below image.

Bind Dataset to DataGridView C#

Step 6: Here we create new connection for database connectivity. Click on button “New Connection” as below image.

Bind Dataset to DataGridView C#

Step 7: A new wizard “Add Connection” is display. See below image and read content according assign point.

 

Bind Dataset to DataGridView C#

 1.       Here you set “Server Name”. For example xyz\SQLExpress or .\SQLExpress.

 2.       If your database is SQL Server Authentication than it required that user name and password.

 3.       In Connect to a database section you have given database. For example my database name is myDatabase. Otherwise you select Windows Authentication.

 4.       If server name, user name, password and database name are correct than it display a confirmation message box as below image.

Bind Dataset to DataGridView C#

 

 5.       Now press button OK and move next step.

Step 8: Here you see (below image). 1 represent new connection name. 2 represent connection string and 3 move for next step.

Bind Dataset to DataGridView C#

 

 

Step 9: Here you save you connection. And click button “Next”.

Bind Dataset to DataGridView C#

Step 10: In this step select table and column according your requirement (1). Give DataSet name (2) and click button “Finish” (3).

Bind Dataset to DataGridView C#

Step 11:  Adjust height and width of datagridview and save application and execute. My demo screen shot given below.

Bind Dataset to DataGridView C#


Datagridview update xml

By Zachary Schulze on   3 years ago
Hello I have ready many of your articles. Do you think you could help me out? Or write a article on loading a xml to a datagridview on application load and save/update xml on application close? I have been lost in the source. This is what I have.

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
DataSet^ myDataSet = gcnew DataSet();
myDataSet->ReadXml("cmrlist.xml");
dataGridView1->DataSource = myDataSet->Tables[0]->DefaultView;
myDataSet->Clear();
myDataSet->ReadXml("ecrlist.xml");
dataGridView2->DataSource = myDataSet->Tables[0]->DefaultView;
}
private: System::Void Form1_Closed(System::Object^  sender, System::EventArgs^  e) {
DataTable^ cmr = gcnew DataTable("CMR");
DataTable^ ecr = gcnew DataTable("ECR");
DataTable^ dt = gcnew DataTable("MAINT");
DataRow^ myrow ;
int icols;
for(int i=0; i<dataGridView1->ColumnCount; i++){
cmr->Columns->Add(dataGridView1->Columns[i]->Name);
}
icols = dataGridView1->Columns->Count; 
for each (DataGridViewRow^ drow in dataGridView1->Rows) { 
myrow = cmr->NewRow(); 
for (int i = 0; i <= icols - 1; i++) { 
myrow[i] = drow->Cells[i]->Value; 
cmr->Rows->Add(myrow);
}
cmr->WriteXml("cmrlist.xml");
//
for(int i=0; i<dataGridView2->ColumnCount; i++){
ecr->Columns->Add(dataGridView2->Columns[i]->Name);
}
icols = dataGridView2->Columns->Count; 
for each (DataGridViewRow^ drow in dataGridView2->Rows) { 
myrow = ecr->NewRow(); 
for (int i = 0; i <= icols - 1; i++) { 
myrow[i] = drow->Cells[i]->Value; 
ecr->Rows->Add(myrow);
}
ecr->WriteXml("ecrlist.xml");

   }

Don't want to miss updates? Please click the below button!

Follow MindStick