kamran haider
kamran haider

Reputation: 11

Update a DataGridView column value in C#

I have created a grid with 6 columns all are of type string now my requirement is when the user double clicks on the 6th column a new form will open and return the value from the selected record in id, name variables.

It is opening the form and getting the values but when executing the following line

grdItemDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = id;

it is not updating the field give the error and close how to update the value for this column please help. The full code is as follows.

private void grdItemDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
        if (e.ColumnIndex == 6)
        {
            frmWarehouseList frmWarehouseList = new frmWarehouseList();
            frmWarehouseList.ShowDialog();
            string id = frmWarehouseList.SelectedWarehouseID;
            string name = frmWarehouseList.SelectedWarehouseName;
            //MessageBox.Show(grdItemDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            grdItemDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = id;
        }

Upvotes: 1

Views: 4654

Answers (1)

LarsTech
LarsTech

Reputation: 81675

The column index is zero based, so if you have six columns, the highest index is five.

Quick fix:

if (e.ColumnIndex == 5)
{
  // etc
}

Upvotes: 3

Related Questions