DataGridView in Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This lesson describes the DataGridView control
This control displays a table-like grid display while the data can come from a data source such as a DataSet or BindingSource.
Of course, you can add rows and columns directly to the control without using the mentioned data source objects.
The DataGridView provides many properties for customizing appearance of the grid.
For example, you can change column header styles and cell border styles, determine whether rows and columns are resizable, and determine whether the control displays tooltips and errors in data cells.
Visual Basic can automatically create a DataGridView bound to a BindingSource and associated with a BindingNavigator.
To do this, create a data source and drag a table from the Data Sources window onto a form.
First click the little "Add New Data Source" button in the Data Source window, and then in the new dialog choose the database as data source type:
Click next so you can select the data you want to retrieve:
Now just drag and drop the table to the form:
Note: you could do the very same with the code below:
Dim table As New DataTable Dim connection As New SqlConnection(My.Settings.ConnectionString) Try connection.Open() Dim command As SqlCommand = connection.CreateCommand command.CommandText = "SELECT * FROM Students" table.Load(command.ExecuteReader) command.Dispose() StudentsGrid.DataSource = table Catch ex As Exception MessageBox.Show(ex.Message) Finally connection.Close() End TryPrevious Lesson | Next Lesson