Tuesday, 10 February 2015

How to show data in a nested DataGrid using C# Windows Forms



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 data/ rows.


  • 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.


After add the datagrid control and work.


1.C# Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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(1, "Manish", "2001");
            dtstudent.Rows.Add(2, "Ram   ", "2002");
            dtstudent.Rows.Add(3, "Ajay Ji", "2003");
            dtstudent.Rows.Add(4, "NIKHIL", "2004");
            // data for devesh ID=111
            dtstudentMarks.Rows.Add(1, "01", "DBMS", 99);
            dtstudentMarks.Rows.Add(1, "02", "ASP.NET", 77);
            dtstudentMarks.Rows.Add(1, "03", "C#", 100);
            dtstudentMarks.Rows.Add(1, "01", "VB", 99);
            //data for ROLI ID=222
            dtstudentMarks.Rows.Add(2, "01", "DBMS", 80);
            dtstudentMarks.Rows.Add(2, "02", "ASP.NET", 95);
            dtstudentMarks.Rows.Add(2, "03", "C#", 95);
            dtstudentMarks.Rows.Add(2, "01", "VB", 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];  
        }
    }
}


2. Figure like this

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




3. After Click Details datgrid show like this




Keep It Up.

No comments:

Post a Comment

Working with 3- Tier Architecture in C#

  Introduction In this article we will learn Use to 3- Tier architecture in C#.NET application. 3-Tier architecture is very famous and ...