This section will explain about creating simple Windows Forms GridControl and the overview of its basic functionalities.
Windows Forms GridControl can be added through code-behind by following the below steps.
- Create a new Windows Form Application.
- Add the below assemblies into the project file
- Syncfusion.Grid.Windows.dll
- Syncfusion.Grid.Base.dll
- Syncfusion.Shared.Base.dll
- Syncfusion.Shared.Windows.dll
Initialize a GridControl by using the below code in code behind.
//Initializing a new Grid.
private Syncfusion.Windows.Forms.Grid.GridControl gridControl1 = new Syncfusion.Windows.Forms.Grid.GridControl();Use the below code for adding the initialized GridControl to the application.
//Add required size for the Grid.
this.gridControl1.Size = new System.Drawing.Size(344, 250);
this.Controls.Add(this.gridControl1);Windows Forms GridControl is a cell based control and hence to populate the GridControl, RowCount and ColCount are necessary. By default the RowCount and ColCount values are 10. Data can be populated by any one of the following methods.
Populate data by looping through cells in GridControl.
//Specifying row and column count
gridControl1.RowCount = 15;
gridControl1.ColCount = 4;
//Looping through the cells and assigning the values based on row and column index
for (int row = 1; row <= gridControl1.RowCount; row++)
{
for (int col = 1; col <= gridControl1.ColCount; col++)
{
gridControl1.Model[row, col].CellValue = string.Format("{0}/{1}", row, col);
}
}In Windows Forms GridControl, each cell contains distinct style information and can be customized independently using GridStyleInfo objects.
To edit cell styles in Designer mode, select the grid and click Edit to access the designer surface. Use the PropertyGrid to modify appearance and style settings for the whole grid or selected range of cells.
Use GridStyleInfo class to customize cell appearance and apply the style to desired range of cells.
//Creates GridStyleInfo object.
GridStyleInfo style = new GridStyleInfo();
//Set properties to it.
style.BackColor = Color.DarkGreen;
style.TextColor = Color.White;
style.Font.Facename = "Verdana";
style.Font.Bold = true;
style.Font.Size = 9f;
//Apply the style to desired range of cells.
this.gridControl1.ChangeCells(GridRangeInfo.Cells(2, 2, 4, 2), style);Windows Forms GridControl provides two types of selection: Range selection (cell-based) and Record selection (record-based).
For record selection, set the ListBoxSelectionMode property:
this.gridControl1.ListBoxSelectionMode = SelectionMode.None;For range selection, set the AllowSelection property:
this.gridControl1.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.Any;Selection operations can be handled using SelectionChanging and SelectionChanged events.
By default the GridControl is in editable state. Use the ReadOnly property to enable or disable editing for the whole grid or individual cells.
// Disabling Edit mode for the whole Grid.
this.gridControl1.ReadOnly = false;
// Disabling edit mode for a particular cell.
GridStyleInfo style = gridControl1.Model[2, 2];
style.ReadOnly = false;Editing operations can be customized using CurrentCellStartEditing and CurrentCellEditingComplete events.
Refer to the Syncfusion WinForms GridControl documentation: Getting Started Guide