Home > DeveloperSection > Beginner > Nested Datagrid in C# winforms

Nested Datagrid in C# winforms


ASP.Net ASP.Net 
Ratings:
2 Comment(s)
 11319  View(s)
Rate this:

Nested Datagrid in C# winforms

 

Introduction

Here I will describe how to show data in nested datagrid using C# winforms

This is applicable to those cases where we have to display master and child content / rows.

Example Student Table

Here in this example we have list of Students  and inside we have details of student marks

StudentId

Student name

Student Roll number

1

Devesh omar

0302713014

 

SubjectID

SubjectName

Marks                           

123

Physics

75

321

chemistry

99

555

Maths

100

 

 

 

2

Nikhil

03027130145

 

SubjectID

SubjectName

Marks                           

123

Physics

72

321

C#

100

555

Maths

100

 

 

 

 

Our purpose is to display this type of data using C#  Winforms

 

Steps and Code:

  1. First we need to create Master and child tables.
  2. To identify the relationship between tables we should have Primary and Foreign Keys in tables 
  3. Here we are creating datatable for master and child tables.
  4. Master table  (student)

                //Parent table

            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));

 

  1. Child Table (Student Marks)

//Child table

            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));

 

f.  Understanding Relation ship between Master and child table

Nested Datagrid in C# winforms  

g.  In brief

dtstudent.StudentID = dtstudentMarks.StudentID

 

  1. Adding Rows to Master table (Student Table)

//Adding Rows

 

            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");

 

i.  Adding Row to Child table (Student Marks table)

   // data for devesh ID=111

            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);

 

 

            //data for ROLI ID=222

            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);

 

  1. Adding master and child table to dataset.

DataSet dsDataset = new DataSet();

           dsDataset.Tables.Add(dtstudent);

            dsDataset.Tables.Add(dtstudentMarks);

  1. Defining relationship between Master and child table

DataRelation Datatablerelation = new DataRelation("DetailsMarks", dsDataset.Tables[0].Columns[0], dsDataset.Tables[1].Columns[0],true);

            dsDataset.Relations.Add(Datatablerelation);

 

l.  Adding Datagrid control in winforms

As per screen below we can add datagrid to winforms

Right click in tool bar -> choose item

 

 

Nested Datagrid in C# winforms

 

m.    Binding data

dataGrid1.DataSource = dsDataset.Tables[0];

 

n.      All code together

private void Form1_Load(object sender, EventArgs e)

        {

            //Parent table

            DataTable dtstudent = new DataTable();

            // add column to datatable  

            dtstudent.Columns.Add("Student_ID"typeof(int));

            dtstudent.Columns.Add("Student_Name"typeof(string));

            dtstudent.Columns.Add("Student_RollNo"typeof(string));

 

           

            //Child table

            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));

           

 

 

            //Adding Rows

 

            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");

 

                // data for devesh ID=111

            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);

 

 

            //data for ROLI ID=222

            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();

            //Add two DataTables  in 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];

 

        }

o.      Running code

We will get following screen having expandable rows in datagrid.

Nested Datagrid in C# winforms

p.      Expanding row to see details

Nested Datagrid in C# winforms

 

q.      Click on DetailsMarks to get Marks details

Nested Datagrid in C# winforms

 

r.        Details screen for student_Id=222

 

Nested Datagrid in C# winforms

 

s.       Go to back for Master details

Click on highlighted back button to go master detail form

Nested Datagrid in C# winforms

 

t.        Main form

Nested Datagrid in C# winforms

 

Conclusion

 

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


Keep Writing

By John Smith on   2 years ago
Keep Writing Devesh.... Good

my problem

By yalda movasseghi on   6 months ago
hi dear friends
fortunately i could use this article for my program but i had some problems .because i have been using data grid view in my program that every cell of that filled  by a value after computations when my program had  been running .so i did not use data source for showing data in data grid view .in addition, at the moment , i want to connect my data grid view with a datatable.
please help me
best regards 

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

Follow MindStick