Introduction
Here I will describe how to show data in a nested DataGrid using C# Windows Forms.
This is applicable to those cases where we need to display master and child content / rows.
Example Student Table
Here in this example we have a list of Students and inside we have details of student marks:
Our purpose is to display this type of data using C# Windows Forms.
Steps and Code
- First we need to create the master and child tables.
- To identify the relationship between tables we should have Primary and Foreign Keys in the tables.
- Here we are creating a datatable for the master and child tables.
- Master table (student)
-
- DataTable dtstudent = new DataTable();
- dtstudent.Columns.Add("Student_ID", typeof(int));
- dtstudent.Columns.Add("Student_Name", typeof(string));
- dtstudent.Columns.Add("Student_RollNo", typeof(string));
- Child Table (Student Marks)
-
- DataTable dtstudentMarks = new DataTable();
- dtstudentMarks.Columns.Add("Student_ID", typeof(int));
- dtstudentMarks.Columns.Add("Subject_ID", typeof(int));
- dtstudentMarks.Columns.Add("Subject_Name", typeof(string));
- dtstudentMarks.Columns.Add("Marks", typeof(int));
- Understanding the relationship between the master and child tables.
- In brief.
- dtstudent.StudentID = dtstudentMarks.StudentID
- Adding Rows to the master table (Student Table).
-
- dtstudent.Rows.Add(111, "Devesh", "03021013014");
- dtstudent.Rows.Add(222, "ROLI", "0302101444");
- dtstudent.Rows.Add(333, "ROLI Ji", "030212222");
- dtstudent.Rows.Add(444, "NIKHIL", "KANPUR");
- Adding a row to the child table (Student Marks table).
-
- dtstudentMarks.Rows.Add(111, "01","Physics", 99);
- dtstudentMarks.Rows.Add(111, "02","Maths", 77);
- dtstudentMarks.Rows.Add(111, "03","C#", 100);
- dtstudentMarks.Rows.Add(111, "01","Physics", 99);
-
-
- dtstudentMarks.Rows.Add(222, "01", "Physics", 80);
- dtstudentMarks.Rows.Add(222, "02", "English", 95);
- dtstudentMarks.Rows.Add(222, "03", "Commerce", 95);
- dtstudentMarks.Rows.Add(222, "01", "BankPO", 99);
- Adding the master and child tables to the dataset.
- DataSet dsDataset = new DataSet();
- dsDataset.Tables.Add(dtstudent);
- dsDataset.Tables.Add(dtstudentMarks);
- Defining the relationship between the master and child tables.
- DataRelation Datatablerelation = new DataRelation("DetailsMarks", dsDataset.Tables[0].Columns[0], dsDataset.Tables[1].Columns[0], true);
- dsDataset.Relations.Add(Datatablerelation);
- The following describes adding a Datagrid control in Windows Forms.
As in the screen below we can add a datagrid to Windows Forms.
Right-click in the toolbar then choose item.
- Binding data.
- dataGrid1.DataSource = dsDataset.Tables[0];
- All the code together.
- private void Form1_Load(object sender, EventArgs e)
- {
-
- DataTable dtstudent = new DataTable();
-
- dtstudent.Columns.Add("Student_ID", typeof(int));
- dtstudent.Columns.Add("Student_Name", typeof(string));
- dtstudent.Columns.Add("Student_RollNo", typeof(string));
-
- DataTable dtstudentMarks = new DataTable();
- dtstudentMarks.Columns.Add("Student_ID", typeof(int));
- dtstudentMarks.Columns.Add("Subject_ID", typeof(int));
- dtstudentMarks.Columns.Add("Subject_Name", typeof(string));
- dtstudentMarks.Columns.Add("Marks", typeof(int));
-
-
- dtstudent.Rows.Add(111, "Devesh", "03021013014");
- dtstudent.Rows.Add(222, "ROLI", "0302101444");
- dtstudent.Rows.Add(333, "ROLI Ji", "030212222");
- dtstudent.Rows.Add(444, "NIKHIL", "KANPUR");
-
- dtstudentMarks.Rows.Add(111, "01","Physics", 99);
- dtstudentMarks.Rows.Add(111, "02","Maths", 77);
- dtstudentMarks.Rows.Add(111, "03","C#", 100);
- dtstudentMarks.Rows.Add(111, "01","Physics", 99);
-
- dtstudentMarks.Rows.Add(222, "01", "Physics", 80);
- dtstudentMarks.Rows.Add(222, "02", "English", 95);
- dtstudentMarks.Rows.Add(222, "03", "Commerce", 95);
- dtstudentMarks.Rows.Add(222, "01", "BankPO", 99);
-
- DataSet dsDataset = new DataSet();
-
-
- dsDataset.Tables.Add(dtstudent);
- dsDataset.Tables.Add(dtstudentMarks);
- DataRelation Datatablerelation = new DataRelation("DetailsMarks", dsDataset.Tables[0].Columns[0], dsDataset.Tables[1].Columns[0], true);
- dsDataset.Relations.Add(Datatablerelation);
- dataGrid1.DataSource = dsDataset.Tables[0];
- }
- Now to run the code.
We will get the following screen having expandable rows in the DataGrid.
Expanding a row to see the details.
- Click on DetailsMarks to get the Marks details.
- Details screen for student_Id=222.
- Go back for the master details.
Click on the highlighted back button to go the master detail form.
- Main form.
We have learned how to show data in a DataGrid with a master and child relationship.