C# DataGridView Database Operations

The versatile DataGridView control offers multiple modes of data display, including Bound mode, unbound mode, and Virtual mode, each catering to distinct requirements and offering unique benefits.

Bound mode

Bound mode stands as an ideal choice when dealing with data management that necessitates seamless interaction with the underlying data store. It facilitates automatic synchronization between the DataGridView and the data source, streamlining the process of managing and updating large datasets. A prevalent scenario where the DataGridView excels in Bound mode is its ability to effortlessly bind to a database table, enabling developers to effortlessly present and manipulate data from a database within the control's tabular interface.

Unbound mode

In contrast, unbound mode proves invaluable when dealing with relatively smaller datasets that are programmatically managed. By opting for unbound mode, developers gain full control over the data displayed within the DataGridView. This mode allows for the manual population of data, making it an ideal choice for scenarios where data manipulation and presentation require intricate programmatic handling. Unbound mode empowers developers to dictate the content and structure of the DataGridView, enabling tailored data display and manipulation to meet specific requirements.

datagridview-database

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

Full Source C#
using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlCommand sCommand; SqlDataAdapter sAdapter; SqlCommandBuilder sBuilder; DataSet sDs; DataTable sTable; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Stores"; SqlConnection connection = 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; } private void new_btn_Click(object sender, EventArgs e) { dataGridView1.ReadOnly = false; save_btn.Enabled = true; new_btn.Enabled = false; delete_btn.Enabled = false; } private void delete_btn_Click(object sender, EventArgs e) { if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); sAdapter.Update(sTable); } } private void save_btn_Click(object sender, EventArgs e) { sAdapter.Update(sTable); dataGridView1.ReadOnly = true; save_btn.Enabled = false; new_btn.Enabled = true; delete_btn.Enabled = true; } } }

For developers seeking a higher level of control over the display and content of cells, Virtual mode presents itself as an optimal choice. By using Virtual mode, developers have the ability to defer the retrieval and display of cell values until the cell is actually being rendered on the screen. This granular control allows for dynamic generation and provision of cell values, offering enhanced performance and responsiveness. Virtual mode is particularly advantageous in scenarios where large datasets are involved, as it reduces memory consumption by only retrieving and rendering the necessary data as it becomes visible to the user.

Conclusion

Offering these diverse modes of data display, the DataGridView control caters to a broad spectrum of requirements, granting developers the freedom to choose the approach that best aligns with their specific needs. Whether it involves seamless integration with a data store, programmatic data management, or fine-grained control over cell rendering, the DataGridView serves as a powerful tool to facilitate effective and efficient tabular data presentation and manipulation within Windows Forms applications.