|
1 | | -# how-to-create-grid-control-in-vb-net |
2 | | -This example demonstrates how to create grid control application in vb.net |
| 1 | +# How to create grid control in vb net |
| 2 | + |
| 3 | +This example demonstrates how to create grid control application in vb.net. |
| 4 | + |
| 5 | +### Creating Grid Control in VB.Net |
| 6 | +1. Create a new VB.Net WPF application project |
| 7 | +2. Install the [Syncfusion.Grid.WPF](https://www.nuget.org/packages/Syncfusion.Grid.WPF) NuGet package as a reference to your .NET Framework applications from NuGet.org. |
| 8 | +3. Add the following Syncfusion namespace in MainWindow.xaml to make use of the GridControl |
| 9 | +4. Add the GridControl inside the `ScrollViewer` control which provides scrollable area to other visible elements that it contains. |
| 10 | + |
| 11 | +#### XAML |
| 12 | + |
| 13 | +``` xml |
| 14 | +<Window x:Class="MainWindow" |
| 15 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 16 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 17 | + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
| 18 | + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
| 19 | + xmlns:local="clr-namespace:GridControlDemo" |
| 20 | + mc:Ignorable="d" |
| 21 | + xmlns:Syncfusion="clr-namespace:Syncfusion.Windows.Controls.Grid;assembly=Syncfusion.Grid.WPF" |
| 22 | + Title="MainWindow" Height="450" Width="800"> |
| 23 | + <Grid> |
| 24 | + <ScrollViewer HorizontalScrollBarVisibility="Visible"> |
| 25 | + <Syncfusion:GridControl x:Name="gridControl"/> |
| 26 | + </ScrollViewer> |
| 27 | + </Grid> |
| 28 | +</Window> |
| 29 | +``` |
| 30 | + |
| 31 | +### Defining Rows and Columns |
| 32 | + |
| 33 | +Users can add the number of rows and columns in grid control by using [RowCount](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.Grid.GridModel.html#Syncfusion_Windows_Controls_Grid_GridModel_RowCount) and [ColumnCount](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.Grid.GridModel.html#Syncfusion_Windows_Controls_Grid_GridModel_ColumnCount) properties. |
| 34 | + |
| 35 | +``` vb |
| 36 | +'Specifying row and column count |
| 37 | +gridControl.Model.RowCount = 50 |
| 38 | +gridControl.Model.ColumnCount = 10 |
| 39 | +``` |
| 40 | + |
| 41 | +### Populating Data |
| 42 | + |
| 43 | +Data can be populated in grid control using one of the following methods. |
| 44 | + |
| 45 | +1. Populate data by looping through the cells in Grid |
| 46 | +``` vb |
| 47 | +'Specifying row and column count |
| 48 | +gridControl.Model.RowCount = 50 |
| 49 | +gridControl.Model.ColumnCount = 10 |
| 50 | + |
| 51 | +Dim r As New Random() |
| 52 | + |
| 53 | +For row As Integer = 1 To 49 |
| 54 | + For col As Integer = 1 To 9 |
| 55 | + gridControl.Model(row, col).CellValue = r.Next(10, 100) |
| 56 | + Next col |
| 57 | +Next row |
| 58 | +``` |
| 59 | + |
| 60 | +2. Populate data by handling the [QueryCellInfo](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Controls.Grid.GridModel.html#Syncfusion_Windows_Controls_Grid_GridModel_QueryCellInfo) event of Grid (Virtual Mode). This will load the data in and on-demand basis, ensuring optimized performance. |
| 61 | + |
| 62 | +``` vb |
| 63 | +'Specifying row and column count |
| 64 | +gridControl.Model.RowCount = 50 |
| 65 | +gridControl.Model.ColumnCount = 10 |
| 66 | +AddHandler gridControl.QueryCellInfo, AddressOf grid_QueryCellInfo |
| 67 | + |
| 68 | +Private Sub grid_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) |
| 69 | + If e.Style.RowIndex = 0 AndAlso e.Style.ColumnIndex = 0 Then |
| 70 | + Return |
| 71 | + 'set value for column headers |
| 72 | + ElseIf e.Style.RowIndex = 0 Then |
| 73 | + e.Style.CellValue = GridRangeInfo.GetAlphaLabel(e.Cell.ColumnIndex) |
| 74 | + 'set value for row headers |
| 75 | + ElseIf e.Style.ColumnIndex = 0 Then |
| 76 | + e.Style.CellValue = e.Style.RowIndex |
| 77 | + 'set value for cells |
| 78 | + Else |
| 79 | + e.Style.CellValue = rand.Next(10, 100) |
| 80 | + End If |
| 81 | +End Sub |
| 82 | +``` |
0 commit comments