简体   繁体   中英

How to transfer data from datagridview cell to and label?

I have two Forms in my school project, and idea is to, when i double click on DataGridView row(that is in Form1) to go to Form2 where i have labels that are empty, and need to set them to be equal to data in cells of that row that i double clicked. DataGridView is populated from MySql Server Table.

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

I tried like this but it doesnt work, it reports error in label, any idea how to make this work on this-kinda-cross Forms?

I have just create a sample, you can do like this

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

In Form2.Designer, you change from private to public

public System.Windows.Forms.Label label1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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