在C#Windows窗体应用程序

时间:2017-08-20 11:42:36

标签: c# winforms datagridview

我有两种表单form1form2。在form1中,我从数据库中获取数据并使用此数据填充“DataGridView”。

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;

然后,我通过单击grid

创建事件处理程序方法以获取customerid
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int customer_id = list_customers[e.RowIndex].customerid;
            MessageBox.Show((customer_id).ToString());
        }

现在,我需要将此customer_id发送到form2以获取所有信息并显示。

知道我是怎么做到的吗?请帮忙

1 个答案:

答案 0 :(得分:1)

您可以在创建第二个表单的实例时传递customer_id

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

以第二种形式,您可以在构造函数中检索此值,

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