Database operations in DatagridView

The DataGridView control offers the capability to present rows of data obtained from a data source. It provides three distinct modes for displaying data: Bound mode, unbound mode, and Virtual mode.

Bound mode

Bound mode is particularly useful when dealing with large datasets and seamless interaction with the underlying data store. By binding the DataGridView control to a table in a database, you can effortlessly manage and manipulate the data through automatic synchronization. This mode is commonly employed when working with extensive databases or when there is a need for real-time updates and synchronization between the DataGridView and the data source.

Unbound mode

On the other hand, unbound mode is well-suited for showcasing smaller amounts of data that you can manually manage and manipulate programmatically. It allows you to populate the DataGridView control directly with data that you generate or retrieve from various sources, such as in-memory collections or custom data structures. This mode offers more flexibility in terms of data manipulation and presentation, making it suitable for scenarios where you have control over the data and its display.

Virtual mode

Virtual mode, as the name suggests, provides a heightened level of control over the data display. It allows you to dynamically provide the values for each cell as they are being displayed on the screen. Instead of preloading all the data into the DataGridView control, virtual mode enables you to wait until a specific cell is being rendered before determining its value. This mode is advantageous when dealing with extensive datasets or when the data retrieval process is resource-intensive, as it offers a more efficient and responsive user experience.


By offering these distinct modes, the DataGridView control caters to various data presentation and manipulation requirements, providing developers with the flexibility to choose the most suitable mode based on the size of the dataset, the level of control desired, and the interaction with the data source.

The following source code illustrate how to connect a DataGridView to a database and addnew/update or delete the database values from DataGridView.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Dim sCommand As SqlCommand Dim sAdapter As SqlDataAdapter Dim sBuilder As SqlCommandBuilder Dim sDs As DataSet Dim sTable As DataTable Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True" Dim sql As String = "SELECT * FROM Stores" Dim connection As New SqlConnection(connectionString) connection.Open() sCommand = New SqlCommand(sql, connection) sAdapter = New SqlDataAdapter(sCommand) sBuilder = New SqlCommandBuilder(sAdapter) sDs = New DataSet() sAdapter.Fill(sDs, "Stores") sTable = sDs.Tables("Stores") connection.Close() DataGridView1.DataSource = sDs.Tables("Stores") DataGridView1.ReadOnly = True save_btn.Enabled = False DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect End Sub Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click DataGridView1.[ReadOnly] = False save_btn.Enabled = True new_btn.Enabled = False delete_btn.Enabled = False End Sub Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index) sAdapter.Update(sTable) End If End Sub Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click sAdapter.Update(sTable) DataGridView1.[ReadOnly] = True save_btn.Enabled = False new_btn.Enabled = True delete_btn.Enabled = True End Sub End Class