Salman Mushtaq
Salman Mushtaq

Reputation: 341

Send Data Grid View data to other form in C# Windows Form Application

I have two forms form1 and form2. In form1 I get data from database and fill `DataGridView' with this data.

Customer customer = new Customer();
var allcustomers = from c in dc.GetTable<Customer>() select new GetCustomers { firstname = c.firstname, lastname = c.lastname, mobile = c.mobile , customerid = c.customerid};
customerBindingSource.DataSource = allcustomers;
dataGridView1.DataSource = customerBindingSource;

Then, I create event handler method to get customerid by clicking on grid

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int customer_id = list_customers[e.RowIndex].customerid;
            MessageBox.Show((customer_id).ToString());
        }

Now, I need to send this customer_id to form2 to get all information and display.

Any idea how I achieve this? Please help

Upvotes: 0

Views: 675

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

You can pass the customer_id while creating an instance of the second form

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
     int customer_id = list_customers[e.RowIndex].customerid;
     SecondForm form2 = new SecondForm(customer_id);
}

and in second form you can retrieve this value in constructor,

 public Form_2(string custId)
 {
    InitializeComponent();
    var customerId = custId;
 }  

Upvotes: 2

Related Questions