Nested DataGrid in C# Window Forms

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
  1. First we need to create the master and child tables.
  2. To identify the relationship between tables we should have Primary and Foreign Keys in the tables.
  3. Here we are creating a datatable for the master and child tables.
  4. Master table (student)
    1. //Parent table  
    2. DataTable dtstudent = new DataTable();  
    3. dtstudent.Columns.Add("Student_ID"typeof(int));  
    4. dtstudent.Columns.Add("Student_Name"typeof(string));  
    5. dtstudent.Columns.Add("Student_RollNo"typeof(string)); 
  5. Child Table (Student Marks)
    1. //Child table  
    2.  DataTable dtstudentMarks = new DataTable();  
    3.  dtstudentMarks.Columns.Add("Student_ID"typeof(int));  
    4.   dtstudentMarks.Columns.Add("Subject_ID"typeof(int));  
    5.   dtstudentMarks.Columns.Add("Subject_Name"typeof(string));  
    6.   dtstudentMarks.Columns.Add("Marks"typeof(int)); 
  6. Understanding the relationship between the master and child tables.



  7. In brief.
    1. dtstudent.StudentID = dtstudentMarks.StudentID  
  8. Adding Rows to the master table (Student Table).
    1. //Adding Rows  
    2. dtstudent.Rows.Add(111, "Devesh""03021013014");  
    3. dtstudent.Rows.Add(222, "ROLI""0302101444");  
    4. dtstudent.Rows.Add(333, "ROLI Ji""030212222");  
    5. dtstudent.Rows.Add(444, "NIKHIL""KANPUR"); 
  9. Adding a row to the child table (Student Marks table).
    1. // data for devesh ID=111  
    2. dtstudentMarks.Rows.Add(111, "01","Physics", 99);  
    3. dtstudentMarks.Rows.Add(111, "02","Maths", 77);  
    4. dtstudentMarks.Rows.Add(111, "03","C#", 100);  
    5. dtstudentMarks.Rows.Add(111, "01","Physics", 99);   
    6.   
    7. //data for ROLI ID=222  
    8. dtstudentMarks.Rows.Add(222, "01""Physics", 80);  
    9. dtstudentMarks.Rows.Add(222, "02""English", 95);  
    10. dtstudentMarks.Rows.Add(222, "03""Commerce", 95);  
    11. dtstudentMarks.Rows.Add(222, "01""BankPO", 99); 
  10. Adding the master and child tables to the dataset.
    1. DataSet dsDataset = new DataSet();  
    2. dsDataset.Tables.Add(dtstudent);  
    3. dsDataset.Tables.Add(dtstudentMarks); 
  11. Defining the relationship between the master and child tables.
    1. DataRelation Datatablerelation = new DataRelation("DetailsMarks", dsDataset.Tables[0].Columns[0], dsDataset.Tables[1].Columns[0], true);  
    2.             dsDataset.Relations.Add(Datatablerelation); 
  12. 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.


     
  13. Binding data.
    1. dataGrid1.DataSource = dsDataset.Tables[0]; 
  14. All the code together.
    1. private void Form1_Load(object sender, EventArgs e)  
    2. {  
    3.     //Parent table  
    4.     DataTable dtstudent = new DataTable();  
    5.     // add column to datatable     
    6.     dtstudent.Columns.Add("Student_ID"typeof(int));  
    7.     dtstudent.Columns.Add("Student_Name"typeof(string));  
    8.     dtstudent.Columns.Add("Student_RollNo"typeof(string));                 
    9.     //Child table  
    10.     DataTable dtstudentMarks = new DataTable();  
    11.     dtstudentMarks.Columns.Add("Student_ID"typeof(int));  
    12.     dtstudentMarks.Columns.Add("Subject_ID"typeof(int));  
    13.     dtstudentMarks.Columns.Add("Subject_Name"typeof(string));  
    14.     dtstudentMarks.Columns.Add("Marks"typeof(int));                   
    15.      //Adding Rows  
    16.   
    17.     dtstudent.Rows.Add(111, "Devesh""03021013014");  
    18.     dtstudent.Rows.Add(222, "ROLI""0302101444");  
    19.     dtstudent.Rows.Add(333, "ROLI Ji""030212222");  
    20.     dtstudent.Rows.Add(444, "NIKHIL""KANPUR");   
    21.     // data for devesh ID=111  
    22.     dtstudentMarks.Rows.Add(111, "01","Physics", 99);  
    23.     dtstudentMarks.Rows.Add(111, "02","Maths", 77);  
    24.     dtstudentMarks.Rows.Add(111, "03","C#", 100);  
    25.     dtstudentMarks.Rows.Add(111, "01","Physics", 99);     
    26.     //data for ROLI ID=222  
    27.     dtstudentMarks.Rows.Add(222, "01""Physics", 80);  
    28.     dtstudentMarks.Rows.Add(222, "02""English", 95);  
    29.     dtstudentMarks.Rows.Add(222, "03""Commerce", 95);  
    30.     dtstudentMarks.Rows.Add(222, "01""BankPO", 99);     
    31.   
    32.     DataSet dsDataset = new DataSet();  
    33.     //Add two DataTables  in Dataset   
    34.   
    35.     dsDataset.Tables.Add(dtstudent);  
    36.     dsDataset.Tables.Add(dtstudentMarks);                 
    37.      DataRelation Datatablerelation = new DataRelation("DetailsMarks", dsDataset.Tables[0].Columns[0], dsDataset.Tables[1].Columns[0], true);  
    38.     dsDataset.Relations.Add(Datatablerelation);  
    39.     dataGrid1.DataSource = dsDataset.Tables[0];   

  15. Now to run the code.

    We will get the following screen having expandable rows in the DataGrid.



    Expanding a row to see the details.


     
  16. Click on DetailsMarks to get the Marks details.


     
  17. Details screen for student_Id=222.


     
  18. Go back for the master details.

    Click on the highlighted back button to go the master detail form.


     
  19. Main form.

              

Conclusion

We have learned how to show data in a DataGrid with a master and child relationship.