如何将数据从datagridview单元传输到并标记?

时间:2019-02-17 16:53:58

标签: c#

我在学校的项目中有两个窗体,其想法是,当我双击DataGridView行(在Form1中)转到Form2时,我的标签为空,需要将它们设置为相等到我双击该行的单元格中的数据。从MySql Server表中填充DataGridView。

    private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        //populate the textbox from specific value of the coordinates of column and row.
        Form2.label1.Text = row.Cells[0].Value.ToString();
        Form2.label2.Text = row.Cells[1].Value.ToString();
        Form2.label3.Text = row.Cells[2].Value.ToString();
        Form2.label4.Text = row.Cells[3].Value.ToString();
        Form2.label5.Text = row.Cells[4].Value.ToString();
        Form2.label6.Text = row.Cells[6].Value.ToString();
        Form2.label7.Text = row.Cells[7].Value.ToString();
        Form2.label8.Text = row.Cells[8].Value.ToString();
        Form2.label9.Text = row.Cells[9].Value.ToString();
        Form2.label10.Text = row.Cells[10].Value.ToString();
        Form2.label11.Text = row.Cells[11].Value.ToString();
        Form2.label12.Text = row.Cells[12].Value.ToString();
        Form2.label13.Text = row.Cells[13].Value.ToString();
        Form2.label14.Text = row.Cells[14].Value.ToString();
    }

我尝试过这种方法,但是它不起作用,它报告标签错误,不知道如何在这种交叉表单上进行这项工作?

1 个答案:

答案 0 :(得分:0)

我刚刚创建了一个样本,您可以这样做

 Form2 form2 = new Form2();
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            //populate the textbox from specific value of the coordinates of column and row.
            form2.label1.Text = row.Cells[0].Value.ToString();
            form2.Show();

在Form2.Designer中,您从私有更改为公共

public System.Windows.Forms.Label label1;