Passing Values from DataGridView between Different Forms.

Sometimes we need to transfer all the rows and columns of a DataGridView from one form to another. In this article I will transfer all the binded data of first form’s grid view and bind them to second form’s grid view.

Create two forms, Form1 is parent form and Form2 is child form. Bind data to DataGridView as below:

1

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GridView_Examples_WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = true;
  }

        private void button1_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            List empList = new List();
            empList.Add(new Employee() { EmployeeName = “Sunil”, EmployeeID = “Emp-s1”, Department = “Developement”, Address = “Jaipur” });
    empList.Add(new Employee() { EmployeeName = “Maya”, EmployeeID = “Emp-m2”, Department = “Developement”, Address = “Jaipur” });
            empList.Add(new Employee() { EmployeeName = “Sneha”, EmployeeID = “Emp-s3”, Department = “QA”, Address = “Calcutta” });
            empList.Add(new Employee() { EmployeeName = “Karuna”, EmployeeID = “Emp-k4”, Department = “HR”, Address = “Jaipur” });
            empList.Add(new Employee() { EmployeeName = “Poonam”, EmployeeID = “Emp-p5”, Department = “Production Support”, Address = “Mumbai” });
            empList.Add(new Employee() { EmployeeName = “Varsha”, EmployeeID = “Emp-v6”, Department = “Marketing”, Address = “Delhi” });
            dataGridView1.DataSource = empList;
        }

private void button2_Click(object sender, EventArgs e)
        {
            List<Employee> empList = dataGridView1.DataSource as List<Employee>;
            new Form2(empList).ShowDialog();
        }
    }
    public class Employee
    {
        public string EmployeeName { get; set; }
        public string EmployeeID { get; set; }
        public string Department { get; set; }
        public string Address { get; set; }
    }
}

Note: In the above code you can see that, inside click event of button2 I am passing List object to constructor of Form2.

Form2:

1

using System.Collections.Generic;
using System.Windows.Forms;
namespace GridView_Examples_WindowsForm
{
    public partial class Form2 : Form
    {
        public Form2(List empList)
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = empList;
        }
    }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s