繁体   English   中英

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

[英]How to transfer data from datagridview cell to and label?

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

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

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

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

 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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