Category / Section
How to make the cells into readonly in WinForms GridControl?
2 mins read
Set the cell into readonly
In GridControl, you can make the cell as read only by setting the ReadOnly Property in the GridModel or by using the QueryCellInfo Event
Using GridModel
C#
//particular cell is set to be ReadOnly gridControl1[3, 3].BackColor = Color.Yellow; gridControl1[3, 3].ReadOnly = true; //Using GridRangeInfo, Range of cells is set to be ReadOnly GridStyleInfo style = new GridStyleInfo(); style.ReadOnly = true; style.BackColor = Color.Red; this.gridControl1.ChangeCells(GridRangeInfo.Cells(1, 1, 2, 2), style); //Using GridRangeInfo,the row is set to be Readonly style.BackColor = Color.Blue; this.gridControl1.ChangeCells(GridRangeInfo.Row(4), style); //Using GridRangeInfo, The Column is set to be Readonly style.BackColor = Color.Aqua; this.gridControl1.ChangeCells(GridRangeInfo.Col(4), style);
VB
'particular cell is set to be ReadOnly gridControl1(3, 3).BackColor = Color.Yellow gridControl1(3, 3).ReadOnly = True 'Using GridRangeInfo, Range of cells is set to be ReadOnly Dim style As New GridStyleInfo() style.ReadOnly = True style.BackColor = Color.Red Me.gridControl1.ChangeCells(GridRangeInfo.Cells(1, 1, 2, 2), style) 'Using GridRangeInfo,the row is set to be Readonly style.BackColor = Color.Blue Me.gridControl1.ChangeCells(GridRangeInfo.Row(4), style) 'Using GridRangeInfo, The Column is set to be Readonly style.BackColor = Color.Aqua Me.gridControl1.ChangeCells(GridRangeInfo.Col(4), style)
Using QueryCellInfo
C#
void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) { // particular cell is set to be ReadOnly if (e.RowIndex == 3 && e.ColIndex == 3) { e.Style.BackColor = Color.Yellow; e.Style.ReadOnly = true; } //Using GridRangeInfo,the row is set to be Readonly if (e.RowIndex == 4) { e.Style.BackColor = Color.Blue; e.Style.ReadOnly = true; } //Using GridRangeInfo, The Column is set to be Readonly if (e.ColIndex == 4) { e.Style.BackColor = Color.Aqua; e.Style.ReadOnly = true; } //Using GridRangeInfo, Range of cells is set to be ReadOnly if ((e.RowIndex == 1 || e.RowIndex == 2) && (e.ColIndex == 1 || e.ColIndex == 2)) { e.Style.BackColor = Color.Red; e.Style.ReadOnly = true; } }
VB
Private Sub gridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) ' particular cell is set to be ReadOnly If e.RowIndex = 3 AndAlso e.ColIndex = 3 Then e.Style.BackColor = Color.Yellow e.Style.ReadOnly = True End If 'Using GridRangeInfo,the row is set to be Readonly If e.RowIndex = 4 Then e.Style.BackColor = Color.Blue e.Style.ReadOnly = True End If 'Using GridRangeInfo, The Column is set to be Readonly If e.ColIndex = 4 Then e.Style.BackColor = Color.Aqua e.Style.ReadOnly = True End If 'Using GridRangeInfo, Range of cells is set to be ReadOnly If (e.RowIndex = 1 OrElse e.RowIndex = 2) AndAlso (e.ColIndex = 1 OrElse e.ColIndex = 2) Then e.Style.BackColor = Color.Red e.Style.ReadOnly = True End If End Sub
The following screenshot displays the grid after applying the properties.
Figure 1: Make the cells into ReadOnly
Note:
The read-only cells are shown in different colors.
Samples:
C#: ReadOnlyCells
VB: ReadOnlyCells