2017-06-29 203 views
0

我有一個datagridview,它允許用戶在其上輸入數據。我希望如果當前行中第一個單元格的值爲空/空,則同一行中的所有其他單元格將設置爲readonly = true,一旦用戶在第一個單元格中輸入內容,則將其他單元格設置爲readonly = false。每當將新行添加到datagridview時,我都希望發生這種情況。Datagridview設置單元格只讀,除了第一個單元格

我已經在datagridview的RowsAdded事件中嘗試了這一點,但它沒有設置爲只讀。

 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
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 
    } 
} 
相關問題