You need to use the DataGridView control in the VB.NET language targeting Windows Forms to display and provide an interface to your data. The DataGridView control is an excellent way to display and allow editing for your data, which can then be persisted in the database again. Here we look at some important parts of using the DataGridView control in the VB.NET language, first using the DataSource property, then binding an object collection, assigning cells, getting the current cell, and more.

First, you should add a DataGridView collection to your Windows Forms application by double-clicking on the control name in the Visual Studio designer panel. After you add the control, you can add the Load event on the form, which you can create from the Form's event pane. Here we see how you can use an empty DataTable on the control.
~~~ Program that uses DataGridView (VB.NET) ~~~
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 ClassForm1_Load event handler. This event handler is executed when the program starts up and when the DataGridView control is displayed. The Form1_Load autogenerated subroutine calls into the GetDataTable function, which would return a DataTable from your database in SQL Server.
DataSource property assignment. Assigning the DataSource property on DataGridView does not copy any data, but instead allows the DataGridView to read in the DataTable and display all its contents on the screen in grid form. This is often the most efficient way to populate DataGridView.

When using the DataGridView control in Windows Forms, you should use the lightning bolt panel which allows you to manipulate the events on the control. DataGridView has many events, and this article doesn't describe them all. However, you will often want the CellClick, SelectionChanged, CellDoubleClick, and KeyPress events, along with others depending on your requirements.
Here we look at how you can use an object collection, such as a List(Of String), in your DataGridView using the Visual Basic language. The object collection will be read in by the DataGridView and its properties (get accessors) will be used to display the values on the screen. This is the easiest way to get started with DataGridView.
--- 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
--- Program that uses DataGridView with class (VB.NET) ---
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 ClassClass definition. The program text includes the Public Class Test definition, which encapsulates two properties with backing stores. The names of these properties are Name and Cost, and could be used for an inventory of merchandise. It is important to declare the members as properties so the metadata can be used by the DataGridView.
Initializing List. After the Dim List is allocated, two new Test objects are added to its contents. These two objects are reflected in the DataGridView output to the screen. You can see the four cells from the four values in the example in the screenshot.

Here we note how you can hide the row headers, which are the boxes on the left of the DataGridView control, from appearing on the screen. The screenshot shows what the row headers look like on DataGridView controls. Go to the Visual Studio designer and change the value of RowHeadersVisible to false, and then compile and run the application.
It is possible to change the behavior for tabbing into and out of the DataGridView control in your Windows Forms program. You can change the StandardTab property on the control in the Visual Studio designer. When StandardTab is enabled, the focus will move out of the DataGridView control when the user presses tab.
Here we see that it is possible to manually add rows to the DataGridView control in the VB.NET language. The Rows.Add function will return the index of the newly added row when you call it. After calling Rows.Add, you can use the Item accessor and the column index to assign the Value properly. The example adds a row with two strings to the DataGridView on load.
=== Program that adds rows (VB.NET) ===
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 ClassAdding columns in designer first. The example will throw an exception if you do not have at least two columns in the DataGridView. To add columns to the control, you can use the Columns collection in the designer and use the Add button. After you add the columns, compile and run your program and no exception will be thrown.
See DataGridView Columns, Edit Columns Dialog.
Here we see that it is possible to obtain the location of the current cell in the VB.NET code. One way you can do this is add the SelectionChanged event by using the lightning bolt panel in Visual Studio. Next, in the DataGridView1_SelectionChanged event, you can access the DataGridView1.CurrentCellAddress property. This is a System.Drawing.Point type that has two instance properties, X and Y. You can use the Point itself or just access its properties.
--- Program that gets current cell (VB.NET) ---
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 ClassUsing SelectionChanged. The example shows the SelectionChanged event handler, which is one of the most useful DataGridView events because it signals whenever the user changes the current cell or clicks on any cell. It is ideal to use for accessing the cell address and changing the UI based on some criteria.
It is possible to make the cells in the DataGridView control expand horizontally. You can set the AutoSizeColumnsMode to fill. Also, you can weight certain columns to have larger widths than other columns using the Edit Columns dialog box. This site has more detailed information on the Edit Columns dialog box for DataGridView.
Here we look at how you can handle the events that occur 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. When the CellDoubleClick event occurs and the header was clicked on, the RowIndex property of the DataGridViewCellEventArgs parameter is -1.
~~~ Program that uses double-clicking (VB.NET) ~~~
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 ClassThere are many more problems that can occur when using the DataGridView control and this article is not comprehensive. However, this site has several more articles related to DataGridView and DataTable, mainly based on the C# language. The DataGridView control is the same in VB.NET and C# code, however.
See DataGridView Tips and Secrets.
Here we looked at many code examples of using the DataGridView control in the VB.NET language targeting the .NET Framework. This control is ideal for rendering data to the screen in programs that use collections of objects or databases and DataTable instances. We used the DataSource property, looked at events, tabbing, row headers, and cell usage in the DataGridView control.