Add CheckBox to C# DataGridView

The DataGridView control, along with its associated classes, has been carefully engineered to serve as a versatile and extensible framework, purposefully designed to facilitate the seamless display and editing of tabular data. As a testament to its flexibility, the DataGridView control offers a diverse range of column types, each equipped with corresponding cell types, including TextBox, CheckBox, Image, Button, ComboBox, and Link. This comprehensive selection empowers developers to effortlessly incorporate various input controls and visual representations within the DataGridView, ensuring a rich and dynamic user interface.

CheckBox functionality within a cell

When it comes to integrating CheckBox functionality within a cell, developers have the freedom to determine the cell's Value property explicitly through programmatically written code. This can be achieved by accessing the desired cell within the Cells collection of the corresponding row. Alternatively, the Value property can be set through data binding, using the powerful capabilities of data binding to seamlessly associate the CheckBox state with the underlying data source.

The following C# program shows how to add a CheckBox in Cell of a DataGridView control and set the third row checkbox value as true.

Full Source C#
using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Product ID"; dataGridView1.Columns[1].Name = "Product Name"; dataGridView1.Columns[2].Name = "Product Price"; string[] row = new string[] { "1", "Product 1", "1000" }; dataGridView1.Rows.Add(row); row = new string[] { "2", "Product 2", "2000" }; dataGridView1.Rows.Add(row); row = new string[] { "3", "Product 3", "3000" }; dataGridView1.Rows.Add(row); row = new string[] { "4", "Product 4", "4000" }; dataGridView1.Rows.Add(row); DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn(); dataGridView1.Columns.Add(chk); chk.HeaderText = "Check Data"; chk.Name = "chk"; dataGridView1.Rows[2].Cells[3].Value = true; } } }

Conclusion

Offering multiple avenues for setting the CheckBox value within a DataGridView cell, developers enjoy the flexibility to choose the approach that best aligns with their application's specific requirements and data management strategies. Whether through programmatic code or data binding, the DataGridView control facilitates efficient and intuitive handling of CheckBox values, ensuring a seamless integration of this functionality within the overall tabular data display and editing experience.