1
votes

I have a datagridview and it allows user to enter data on it. I want that if the value of the first Cell in current row is Null/Empty all other cells in the same row is set to readonly=true, once the user enter something in the first cell then set the other cell to readonly=false. I want this to happen every time a new row is added to the datagridview.

I already tried this in RowsAdded event of datagridview but its not setting to readonly.

     if (dgvMaterials.Rows[e.RowIndex].Cells["ItemID"].Value == null)
        {
            dgvMaterials.Rows[e.RowIndex].Cells["Description"].ReadOnly = true;
            dgvMaterials.Rows[e.RowIndex].Cells["Qty"].ReadOnly = true;
            dgvMaterials.Rows[e.RowIndex].Cells["Cost"].ReadOnly = true;
        }
1

1 Answers

2
votes
private void dgvMaterials_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    if (dgvMaterials.Rows[e.RowIndex].Cells["ItemID"].Value != null)
    {
        dgvMaterials.Rows[e.RowIndex].ReadOnly = true;  // set all row as read-only
        dgvMaterials.Rows[e.RowIndex].Cells["ItemID"].ReadOnly = false;  //except ItemID-cell
    }
}

private void dgvMaterials_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dgvMaterials.Columns["ItemID"].Index)  //if the ItemID-cell is edited
    {
        dgvMaterials.Rows[e.RowIndex].ReadOnly = true;  // set all row as read-only
        dgvMaterials.Rows[e.RowIndex].Cells["ItemID"].ReadOnly = false;  //except ItemID-cell
    }
}