| True DBGrid for WinForms Task-Based Help > Controlling Grid Interaction > Locking a Cell from Being Edited |
You may want to prevent the end user from editing the data in particular cells. If you choose, you can lock individual grid cells from being edited at run time by using the FetchCellStyle event.
To lock the value in cell (1, 0), complete the following steps:
In the Designer
In Code
Add the following code to the Form_Load event:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Me.C1TrueDBGrid1.Splits(0).DisplayColumns(0).FetchStyle = True |
|
To write code in C#
| C# |
Copy Code
|
|---|---|
this.c1TrueDBGrid1.Splits[0].DisplayColumns[0].FetchStyle = true; |
|
Set the Locked property of the CellStyle object to True only for the value in row one:
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Private Sub C1TrueDBGrid1_FetchCellStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs) Handles C1TrueDBGrid1.FetchCellStyle
If e.Row = 1 Then
e.CellStyle.Locked = True
End If
End Sub
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
private void C1TrueDBGrid1_FetchCellStyle(object sender, C1.Win.C1TrueDBGrid.FetchCellStyleEventArgs e)
{
if (e.Row == 1)
{
e.CellStyle.Locked = true;
}
}
|
|
The value in the cell (1, 0) cannot be edited: