VB.NET DataGridView binding - Sql Server

The flexibility of the DataGridView control allows for a multitude of approaches to incorporate custom behaviors into your applications. The DataGridView offers three distinct modes of data display: Bound mode, Unbound mode, and Virtual mode. Each mode caters to specific scenarios and provides varying levels of interaction and control.

Bound mode, the most commonly used mode, facilitates seamless management of data through automatic interaction with the underlying data store. This mode is particularly suited for scenarios where the DataGridView control is bound to a table in a database. By establishing a data binding between the control and the database table, changes made in the DataGridView are automatically reflected in the underlying data store, streamlining the data management process.

On the other hand, Unbound mode is ideal for presenting relatively smaller amounts of data that you can manage programmatically. In this mode, you have full control over populating and manipulating the data within the DataGridView. Instead of relying on a direct connection to a data source, you can manually populate the DataGridView with data from various sources, such as collections or custom data structures. This mode grants you greater flexibility in handling and customizing the data displayed in the control.

For the utmost control and efficiency, Virtual mode empowers you to have fine-grained control over the data displayed in the DataGridView. In this mode, the control defers loading the cell values until they are about to be displayed on the screen. This deferred loading approach allows you to dynamically provide the values for each cell, optimizing performance by avoiding unnecessary data retrieval or computation. By implementing custom logic, you can determine the value for each cell precisely when it is needed, offering a high degree of control over the data representation in the DataGridView.

The following vb.net program shows how to bind a SQL Server dataset in a DataGridView.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True" Dim sql As String = "SELECT * FROM Authors" Dim connection As New SqlConnection(connectionString) Dim dataadapter As New SqlDataAdapter(sql, connection) Dim ds As New DataSet() connection.Open() dataadapter.Fill(ds, "Authors_table") connection.Close() DataGridView1.DataSource = ds DataGridView1.DataMember = "Authors_table" End Sub End Class

The DataGridView control's versatility enables you to tailor its behavior to your specific application requirements. Whether through Bound mode, Unbound mode, or Virtual mode, you have the flexibility to manage and display data in a manner that best aligns with your application's needs.