在c#中从datagridview获取记录的ID,并将数据显示到另一个窗体

在c#中从datagridview获取记录的ID,并将数据显示到另一个窗体,c#,C#,我正在构建一个非常基础/入门级的应用程序来学习c#,我想将记录的id传递给另一个表单,在那里我可以通过编写查询来获取记录。 我一直在关注这个youtube教程 但它并没有帮我摆脱困境。我试过的代码是 private void load_data_Click(object sender, EventArgs e) { container_departure_details cdd = new container_departure_details();

我正在构建一个非常基础/入门级的应用程序来学习c#,我想将记录的id传递给另一个表单,在那里我可以通过编写查询来获取记录。 我一直在关注这个youtube教程

但它并没有帮我摆脱困境。我试过的代码是

 private void load_data_Click(object sender, EventArgs e)
    {
        container_departure_details cdd = new container_departure_details();
        cdd.Show();
    }
它显示了下一个表单,但我无法传递并获取第二个表单中的id值

集装箱离港详情


谢谢,试试这个。您可以使用datagrid的CellClick事件获取ID为的单元格的值

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        // get the row index of the cell clicked
        var rowIndex = e.RowIndex;

        //specify 0, if the Id is in the first Column else in place of 0 e.ColumnIndex
        var id = dataGridView1.Rows[e.RowIndex].Cells[0].Value;

        var newForm = new Form();
        newForm.Show();

    }

多亏了唐尼先生,他的答案确实有助于分配,在对他的答案进行了一些小的修改后,我成功地将值转换为其他形式。最后的代码是

public static string SetValueForText1 = "";
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // get the row index of the cell clicked
            var rowIndex = e.RowIndex;

            //specify 0, if the Id is in the first Column else in place of 0 e.ColumnIndex
            var id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

            SetValueForText1 = id;
            var newForm = new container_departure_details();

            newForm.Show();
        }
在我的例子中,在表2中,它是集装箱_出发_详情通过以下行获取值

record_id.Text = inspection_report.SetValueForText1;
检验报告是表格的名称

有关更多详细信息,请访问此链接
您可以在DataGridView中使用SelectionChanged。 例如:

private void dataGridView_SelectionChanged(object sender, EventArgs e) 
{
    foreach (DataGridViewRow row in dataGridView.SelectedRows) 
    {
        string value1 = row.Cells["Id"].Value.ToString();
        //...
    }
}

好的,通过尝试您的代码,它将值传递给newFrom?我怎样才能以新的形式得到这个值呢?再次感谢..我从这篇文章中得到了帮助,发现修改后的解决方案有点轻松,现在工作正常,将在我的commentsok javad中共享最终代码,但如果我没有错的话,我有一个问题,关于SelectionChanged它获取id的值,在每一个SelectionChanged它都会弹出表单2,我想在上面获取id的值?是的,通过这种方式,您可以在每次选择更改时获取当前行的Id并弹出表单2。我想你需要为每一行换一个身份证。