DataGridView
This provides a visual interface
to data. It is an excellent way to display and allow editing for your data. It is accessed with VB.NET code.
You can add a DataGridView
control to your Windows Forms application by double-clicking on the control name in the Visual Studio designer panel.
After you add the DataGridView
, you can add the Load event on the form. You can create the Load event on the Form's event pane in Visual Studio.
DataTable
on the DataGridView
control. We assign the DataSource
property.DataGridView
control is displayed.DataSource
property, the DataGridView
reads in the DataTable
and displays all its contents on the screen in grid form.Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' ' Fill in the data grid on form load. ' DataGridView1.DataSource = GetDataTable() End Sub Private Function GetDataTable() As DataTable ' ' This Function needs to build the data table. ' Return New DataTable() End Function End Class
You can use an object collection, such as a List
(Of String
), in your DataGridView
using the VB.NET language. The object collection will be read.
DataGridView
. But it may be less effective than more complex approaches.Class
Test definition, which encapsulates two properties with backing stores (Name and Cost).DataGridView
.List
. These 2 objects are reflected in the DataGridView
output to the screen.''' <summary> ''' This class contains two properties. ''' </summary> Public Class Test Public Sub New(ByVal name As String, ByVal cost As String) _name = name _cost = cost End Sub Private _name As String Public Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Private _cost As String Public Property Cost() As String Get Return _cost End Get Set(ByVal value As String) _cost = value End Set End Property End ClassPublic Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' ' Fill in the data grid with a List ' Dim list = New List(Of Test) list.Add(New Test("Mac", 2200)) list.Add(New Test("PC", 1100)) DataGridView1.DataSource = list End Sub End Class
Rows.Add()
will return the index of the newly-added row. After calling it, you can use the Item accessor and the column index to assign the Value properly.
DataGridView
on load. These are displayed to the screen.Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Add row using the Add subroutine. Dim n As Integer = DataGridView1.Rows.Add() DataGridView1.Rows.Item(n).Cells(0).Value = "First" DataGridView1.Rows.Item(n).Cells(1).Value = "Second" End Sub End Class
We can obtain the location of the current cell in VB.NET code. One way you can do this is add the SelectionChanged
event.
DataGridView1_SelectionChanged
event, you can access the DataGridView1.CurrentCellAddress
property.System.Drawing.Point
type that has two instance properties, X and Y.Point
itself or just access its properties. Its properties are of type Integer.Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Uses Test class from above. Dim list = New List(Of Test) list.Add(New Test("Mac", 2200)) list.Add(New Test("PC", 1100)) DataGridView1.DataSource = list End Sub Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles DataGridView1.SelectionChanged ' Get the current cell location. Dim y As Integer = DataGridView1.CurrentCellAddress.Y Dim x As Integer = DataGridView1.CurrentCellAddress.X ' Write coordinates to console. Console.WriteLine(y.ToString + " " + x.ToString) End Sub End Class
Double
-clickingYou can detect when the user double
-clicks on a cell. You can open the Visual Studio designer and add the CellDoubleClick
event in the pane with the lightning bolt.
CellDoubleClick
event occurs and the header was clicked on, the RowIndex
of the DataGridViewCellEventArgs
parameter is -1.Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Load here. End Sub Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles DataGridView1.CellDoubleClick If e.RowIndex = -1 Then Return End If ' Double-click logic. End Sub End Class
When using the DataGridView
control in Windows Forms, you should use the lightning bolt panel. This allows you to manipulate the events on the control.
CellClick
, SelectionChanged
, CellDoubleClick
, and KeyPress
events, depending on your requirements.You can hide the row headers—these are the boxes on the left of the control. Go to the Visual Studio designer and change the value of RowHeadersVisible
to false.
It is possible to improve the behavior for tabbing into and out of the DataGridView
control in your Windows Forms program.
StandardTab
property on the control in the Visual Studio designer.StandardTab
is enabled, the focus will move out of the DataGridView
control when the user presses tab.We can use the Columns collection in the designer and use the Add button. After we add the columns, compile and run the program and no exception will be thrown.
It is possible to make the cells in your DataGridView
expand horizontally. You can set the AutoSizeColumnsMode
property to fill.
We looked at the DataGridView
control. This control is ideal for rendering data to the screen in programs that use collections of objects or databases and DataTable
instances.