简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM