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. Due to Virtual mode, developers are able to not only defer the data retrieval but also its visualization until the very moment when a cell becomes visible on the screen. It gives the ability to govern dynamically the creation and distribution of cells' values through to most efficient operation and performance. This virtual approach works great in contexts where huge sets of data are involved, because it results in a decrease in memory size as only the necessary data is displayed on the screen one time 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.