Category / Section
How to set the number of rows and columns in a WinForms GridControl?
2 mins read
Setting row and column count
You can change the Grid’s RowCount and ColCount properties by using the designer. Set these properties after calling InitializeComponent in the form’s constructor or anytime later in your code using the following code example.
Using direct properties
C#
// set RowCount for GridControl gridControl1.RowCount = 20; // set ColCount for GridControl gridControl1.ColCount = 120;
VB
' set RowCount for GridControl gridControl1.RowCount = 20 ' set ColCount for GridControl gridControl1.ColCount = 120
Using Events
The events QueryRowCount and QueryColCount are used to set the count for rows and columns.
C#
//Hook these events to set the RowCol count gridControl1.QueryColCount += gridControl1_QueryColCount; gridControl1.QueryRowCount += gridControl1_QueryRowCount; void gridControl1_QueryColCount(object sender, GridRowColCountEventArgs e) { //Set ColCount for GridControl e.Count = int.Parse(colCountText.Text); e.Handled = true; } void gridControl1_QueryRowCount(object sender, GridRowColCountEventArgs e) { //Set RowCount for GridControl e.Count = int.Parse(rowCountTxt.Text); e.Handled = true; }
VB
//Hook these events to set the RowCol count gridControl1.QueryColCount += gridControl1_QueryColCount; gridControl1.QueryRowCount += gridControl1_QueryRowCount; Private Sub gridControl1_QueryColCount(ByVal sender As Object, ByVal e As GridRowColCountEventArgs) 'Set ColCount for GridControl e.Count = Integer.Parse(colCountText.Text) e.Handled = True End Sub Private Sub gridControl1_QueryRowCount(ByVal sender As Object, ByVal e As GridRowColCountEventArgs) 'Set RowCount for GridControl e.Count = Integer.Parse(rowCountTxt.Text) e.Handled = True End Sub
The following screenshot illustrates the output.
Figure 1: Setting the row, col count using QueryRowCount, QueryColCount events
Samples: