Saturday, 23 August 2014

Nested DataGrid or Expandable and Collapsible Rows in Datagrid in C# Window Forms


Nested DataGrid in Windows Form application


Description:-



In this example we explain that how to create nested Datagrid in window form application using C# and VB .Net or how to create Expand and Collapsible rows in Datagrid in window form application with C#. or create nested datagrid with expand and collapsible rows in C# winforms application.

We already explain that nested gridview example in Asp.Net and even in mvc also but now we explain in windows form application.


Fancy Image Slideshow of Image in Jquery Beautiful Slideshow in Jquery 

Awesome Login Popup in Jquery Signup and Login Popup in Jquery

to create Nested Datagrid in window form application follow the below step.

Right click on the toolbar and choose the item display in below image.


nested datagrid in window form C#


 Now Drag and Drop DataGrid in window Form and write the following code in PageLoad.



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)
        {
            DataTable dt_Employee = new DataTable();

            // add column to datatable 

            dt_Employee.Columns.Add("Emp_ID", typeof(int));

            dt_Employee.Columns.Add("Emp_Name", typeof(string));
            dt_Employee.Columns.Add("Emp_Salary", typeof(string));


            //Child table

            DataTable dt_Department = new DataTable();

            dt_Department.Columns.Add("Emp_ID", typeof(int));
            dt_Department.Columns.Add("Dept_ID", typeof(int));

            dt_Department.Columns.Add("Dept_Name", typeof(string));


            //Adding Rows

            dt_Employee.Rows.Add(1, "kirit", "25000");

            dt_Employee.Rows.Add(2, "pintu", "80000");

            dt_Employee.Rows.Add(3, "Rahul", "54222");



            // Department for kirit ID=1

            dt_Department.Rows.Add(1, "01", "IT");



            //Department for pintu ID=2

            dt_Department.Rows.Add(2, "02", "Account");
            //Department for Rahul ID=3

            dt_Department.Rows.Add(3, "03", "Finance");


            DataSet dsDataset = new DataSet();

            //Add two DataTables  in Dataset


            dsDataset.Tables.Add(dt_Employee);

            dsDataset.Tables.Add(dt_Department);

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

            dsDataset.Relations.Add(Datatablerelation);

            dataGrid1.DataSource = dsDataset.Tables[0];
        }
    }
}


1 comments: