C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Load: You can create the Load event on the Form's event pane in Visual Studio. We use Load in this example.
Here: We use an empty DataTable on the DataGridView control. We assign the DataSource property.
VB.NET program that uses DataGridView
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
Info: Assigning the DataSource property copies no data. The DataGridView reads in the DataTable and displays all its contents on the screen in grid form.
DataTableHowever: You will often want the CellClick, SelectionChanged, CellDoubleClick, and KeyPress events, depending on your requirements.
Tip: This is the easiest way to get started with DataGridView. But it may be less effective than more complex approaches.
Test: The program includes the Public Class Test definition, which encapsulates two properties with backing stores (Name and Cost).
Note: It is important to declare the members as properties so the metadata can be used by the DataGridView.
And: Two new Test objects are added to the List. These 2 objects are reflected in the DataGridView output to the screen.
Class used in program: VB.NET
''' <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 Class
VB.NET program that uses DataGridView with class
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 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
Tip: Go to the Visual Studio designer and change the value of RowHeadersVisible to false. This hides row headers.
So: When StandardTab is enabled, the focus will move out of the DataGridView control when the user presses tab.
Here: The example adds a row with two strings to the DataGridView on load. These are displayed to the screen.
VB.NET program that adds rows
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
Then: After you add the columns, compile and run your program and no exception will be thrown.
Edit ColumnsNext: In the DataGridView1_SelectionChanged event, you can access the DataGridView1.CurrentCellAddress property.
CurrentCellAddress: This is a System.Drawing.Point type that has two instance properties, X and Y.
Tip: You can use the Point itself or just access its properties. Its properties are of type Integer.
SelectionChanged: This signals when the user changes the current cell or clicks on any cell.
VB.NET program that gets current cell
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
Note: When the CellDoubleClick event occurs and the header was clicked on, the RowIndex of the DataGridViewCellEventArgs parameter is -1.
VB.NET program that uses double-clicking
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