Add Button to C# DataGridView

The DataGridView control, along with its associated classes, has been thoughtfully crafted as a versatile and expandable system specifically tailored for the purpose of exhibiting and modifying tabular data. Its inherent flexibility allows developers to seamlessly adapt the control to their unique requirements, ensuring a comprehensive solution for presenting and manipulating data in a structured format.

In addition to its flexibility, the DataGridView control stands out for its remarkable configurability and extensibility. It offers a rich assortment of properties, methods, and events that empower developers to customize every aspect of its appearance and behavior. This extensive toolkit allows for fine-tuning the visual presentation, interaction mechanisms, and data handling functionalities, guaranteeing a highly tailored and engaging user experience.

Add a Button in Cell of a DataGridView control

One of the notable features of the DataGridView control lies in its provision of various column types, each equipped with corresponding cell types. These encompass a diverse range of data input and display mechanisms, including TextBox, CheckBox, Image, Button, ComboBox, and Link columns. This versatile selection of column types enables developers to seamlessly incorporate different data formats, interactivity options, and specialized functionality within the DataGridView control, catering to diverse use cases and enhancing the richness of the user interface.

The following C# program shows how to add a Button in Cell of a DataGridView control. Also it showing in the dataGridView1_CellClick event which button the user clicked.

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); DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); dataGridView1.Columns.Add(btn); btn.HeaderText = "Click Data"; btn.Text = "Click Here"; btn.Name = "btn"; btn.UseColumnTextForButtonValue = true; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) { MessageBox.Show((e.RowIndex+1) + " Row " + (e.ColumnIndex+1) + " Column button clicked "); } } } }

Conclusion

Exploiting the power of the DataGridView control and its associated column types, developers can effortlessly create data-driven interfaces that use a wide range of input controls and visual representations. This comprehensive approach ensures that tabular data is not only displayed accurately but also allows users to interact with and manipulate it efficiently and intuitively.