获取单元格的值并将其转换为第二种形式

时间:2013-12-14 09:17:53

标签: c# winforms datagridview

如何获取单击的单元格的值并将其传输到第二个表单?

到目前为止,这是我的代码:

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 1)
        {
            var form2 = new Form2();
            this.Hide();
            form2.Show();
        }
    }

2 个答案:

答案 0 :(得分:1)

更改将接收值的表单的构造函数,如:

  public Form2(String passCellValue)
    {
        InitializeComponent();
    }

然后这样打电话:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        String cellValue;
        cellValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        var form2 = new Form2(cellValue);
        this.Hide();
        form2.Show();
    }
}

答案 1 :(得分:0)

很容易

private void gridAgentDetails_Click(object sender, EventArgs e) {  for (int i = 0; i < this.gridAgentDetails.CurrentRowIndex; i++)  {      string str = gridAgentDetails.Text; }}

plaese检查链接

How to get column value of grid in C# Windows application?

并传输数据,您可以制作一般属性或变量。请检查此链接。

http://social.msdn.microsoft.com/Forums/vstudio/en-US/08943b7b-9603-49ad-950c-62e8f8325df2/how-to-pass-value-data-grid-view-click-to-another-form?forum=vbgeneral