Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using the code below, I tried to <Dynamically Set DataGridView Cells ReadOnly in order to made the text in the cells not editable. But it does not work Did I do something incorrect? Thanks.
C#
void setDataGridViewRow(DataGridView dgv, int iCase)  {  
   for (int i = 0; i < dgv.RowCount; i++)   {
      DataGridViewRow row = dgv.Rows[i];
      for (int j = 0; j < row.Cells.Count; j++)  {
         if (j < 2 || !IsEditableField(row.Cells[0].Value.ToString()))   {
             row.Cells[j].ReadOnly = true;
             row.Cells[j].ToolTipText = "Not Editable";
         }  else
             row.Cells[j].ReadOnly = false;
         }
      }
   }
}


What I have tried:

Dynamically Set DataGridView Cells ReadOnly 
but not working
Posted
Updated 26-May-17 4:29am
v2

1 solution

Maybe you can try something like this:
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
   if (e.ColumnIndex < 2) 
   { 
      e.Cancel = true;
   }
}
If that does not work try this: Difficulty setting readonly cells in DataGridView[^]
 
Share this answer
 
v2
Comments
s yu 26-May-17 14:20pm    
Solved using the code below:
for (int j = 0; j < row.Cells.Count; j++) {
if (j < 2 || !IsEditableField(row.Cells[0].Value.ToString())) {
row.Cells[j].ReadOnly = true;
row.Cells[j].ToolTipText = "Not Editable";
} else
row.Cells[j].ReadOnly = false;
}
Thanks for your feedback.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900