我有一个datagridview,它允许用户在其上输入数据。我希望如果当前行中第一个Cell的值为Null / Empty,则同一行中的所有其他单元格都设置为readonly = true,一旦用户在第一个单元格中输入内容,则将另一个单元格设置为readonly = false。我希望每次将新行添加到datagridview时都会发生这种情况。
我已经在datagridview的RowsAdded事件中尝试了这个,但是它没有设置为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;
}
答案 0 :(得分:1)
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
}
}