This page was last reviewed on Sep 27, 2022.
Dot Net Perls
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.
Shows a datagridviewShows a datagridview
Get started. You can add a DataGridView control to your Windows Forms application by double-clicking on the control name in the Visual Studio designer panel.
First example. 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.
Here We use an empty DataTable on the DataGridView control. We assign the DataSource property.
Detail The event handler is executed when the program starts up and when the DataGridView control is displayed.
Info When we assign the DataSource property, the DataGridView reads in the DataTable and displays all its contents on the screen in grid form.
Shows a 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
Object example. 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.
Tip This is the easiest way to get started with DataGridView. But it may be less effective than more complex approaches.
Detail 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.
Shows a datagridview
''' <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
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
Add rows. 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.
Here The example adds a row with 2 strings to the 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
Get current cell. We can obtain the location of the current cell in VB.NET code. One way you can do this is add the SelectionChanged event.
Detail You can add events easily in Visual Studio by using the lightning bolt panel.
Next In the DataGridView1_SelectionChanged event, you can access the DataGridView1.CurrentCellAddress property.
Detail 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.
Detail This signals when the user changes the current cell or clicks on any 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
Double-clicking. You 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.
Note When the 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
Lightning bolt. 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.
Important You will often want the CellClick, SelectionChanged, CellDoubleClick, and KeyPress events, depending on your requirements.
Hide row headers. 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.
Improve tabbing. It is possible to improve the behavior for tabbing into and out of the DataGridView control in your Windows Forms program.
Detail You can change the StandardTab property on the control in the Visual Studio designer.
Note When StandardTab is enabled, the focus will move out of the DataGridView control when the user presses tab.
Adding columns, designer. 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.
Expand cells. It is possible to make the cells in your DataGridView expand horizontally. You can set the AutoSizeColumnsMode property to fill.
Also You can weight certain columns to have larger widths than other columns using the Edit Columns dialog box.
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 27, 2022 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.