简体   繁体   中英

How do I display my datagrid view items to textboxes in a another form?

I am trying to display items from my data grid view but when I click the button to get an error saying that the index cannot be negative, must be less than the size of the collection and it is out of bounds. how do I fix this?

private void button2_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;

            Form1 f1 = new Form1();

            MySqlCommand cmd = conn.CreateCommand();

            VehicleEntry edit = new VehicleEntry();

            edit.Show();

            edit.pnumber.Text = f1.dataGridView1[0, row].Value.ToString();


        }

Remove the line declaring new Form 1 if this code is in Form1, and use 'this.dataGridView1[0, row].Value.ToString()' in the last line.

Change to:

 private void button2_Click(object sender, EventArgs e)
 {
      int row = dataGridView1.CurrentRow.Index;
    
      //Form1 f1 = new Form1(); //remove if this is Form1
    
      MySqlCommand cmd = conn.CreateCommand();
    
      VehicleEntry edit = new VehicleEntry();
    
      edit.Show();
      //if datagridview1 is in the current Form.
      edit.pnumber.Text = this.dataGridView1[0, row].Value.ToString();
    
    
 }

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