0
votes

I have a DataGridView and I want to change the value in cells of selected rows to the value selected in a drop-down box. It is always the cells in the 3rd column.

My code is:

private void updateSelected_Click(object sender, EventArgs e)
{
     foreach (DataGridViewRow i in dataGridView1.SelectedRows)
     {
          dataGridView1[2, i].Value = Combo.Text;
     }
     this.BindingContext[dataGridView1.DataSource].EndCurrentEdit();
}

However, I am getting the following error:

CS1503 Argument 2: cannot convert from 'System.Windows.Forms.DataGridViewRow' to 'int'

I have it working to update ALL rows:

    private void updateExcel_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.RowCount - 1; i++)
        {
            if (!RowIsEmpty(i))
            {
                dataGridView1[2, i].Value = Combo.Text;
            }
        }
    }
1
Either change your foreach loop to a for loop or use i.Cells[2].Value instead.41686d6564

1 Answers

1
votes

i is a DataGridViewRow not a row-number:

 foreach (DataGridViewRow i in dataGridView1.SelectedRows)
 {
      i.Cells[2].Value = Combo.Text;
 }