How to create GridControl using VB.NET in WPF
The grid control is a cell-oriented control for displaying tabular data. It does not make any assumptions regarding the structure of the data. Users can customize it down to the cell level and can use a grid virtually where the data is provided on demand in real-time. If you are searching for Grid capable of binding different types of data sources and handling data (sorting, filtering, etc.), please refer to the DataGrid control.
Creating Grid Control in VB.Net
- Create a new VB.Net WPF application project
- Install the Syncfusion.Grid.WPF NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Add the following Syncfusion namespace in MainWindow.xaml to make use of the GridControl.
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GridControlDemo" mc:Ignorable="d" xmlns:Syncfusion="clr-namespace:Syncfusion.Windows.Controls.Grid;assembly=Syncfusion.Grid.WPF" Title="MainWindow" Height="450" Width="800"> <Grid> </Grid> </Window>
Add the GridControl inside the ScrollViewer control which provides scrollable area to other visible elements that it contains.
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GridControlDemo" mc:Ignorable="d" xmlns:Syncfusion="clr-namespace:Syncfusion.Windows.Controls.Grid;assembly=Syncfusion.Grid.WPF" Title="MainWindow" Height="450" Width="800"> <Grid> <ScrollViewer HorizontalScrollBarVisibility="Visible"> <Syncfusion:GridControl x:Name="gridControl"/> </ScrollViewer> </Grid> </Window>
Defining Rows and Columns
Users can add the number of rows and columns in grid control by using RowCount and ColumnCount properties.
'Specifying row and column count gridControl.Model.RowCount = 50 gridControl.Model.ColumnCount = 10
Populating Data
Data can be populated in grid control using one of the following methods.
- Populate data by looping through the cells in Grid.
'Specifying row and column count gridControl.Model.RowCount = 50 gridControl.Model.ColumnCount = 10 Dim r As New Random() For row As Integer = 1 To 49 For col As Integer = 1 To 9 gridControl.Model(row, col).CellValue = r.Next(10, 100) Next col Next row
- Populate data by handling the QueryCellInfo event of Grid (Virtual Mode). This will load the data in and on-demand basis, ensuring optimized performance.
'Specifying row and column count gridControl.Model.RowCount = 50 gridControl.Model.ColumnCount = 10 AddHandler gridControl.QueryCellInfo, AddressOf grid_QueryCellInfo Private Sub grid_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) If e.Style.RowIndex = 0 AndAlso e.Style.ColumnIndex = 0 Then Return 'set value for column headers ElseIf e.Style.RowIndex = 0 Then e.Style.CellValue = GridRangeInfo.GetAlphaLabel(e.Cell.ColumnIndex) 'set value for row headers ElseIf e.Style.ColumnIndex = 0 Then e.Style.CellValue = e.Style.RowIndex 'set value for cells Else e.Style.CellValue = rand.Next(10, 100) End If End Sub
Download grid control demo from GitHub
Editing
Grid control has the default support for editing the cells. Editing can be customized for each cell using various events like `CurentCellStartEditing`, `CurrentCellActivating` and `CurrentCellChanging`, etc. Also, GridControl support various built-in editors such as `DoubleEdit`, `PercentEdit`, `IntegerEdit`, `MaskEdit`, `RichText`, `UpDownEdit`, `CurrencyEdit` and `DateTimeEdit` which can be configured for each cell in GridControl.
Formulas
Grid control supports Excel-like formulas in each cell and allows to enter algebraic expressions using formulas and cell references by setting the cell type of a cell to `FormulaCell`. The control comes with an extensive formula function library that supports more than 150 built-in formulas.
Excel-like features
Grid control has the built-in supports to change the appearance like Microsoft Excel current cell, selection and selection frame by using ExcelLikeCurrentCell, ExcelLikeSelection and ExcelLikeSelectionFrame properties respectively.
Summary
Grid control has the support for various cell types, exporting options, support for serialization, printing support and support to customize the appearance, etc. For more information on the Grid Control and its features, please see our User Guide Documentation. You can also refer to the Feature Tour site to get an overview on all the features in grid. Refer this documentation to know about the required assemblies for creating grid control.