This program shows how to add checkbox in a DataGridView control . First you need to create New Project and give the Name of project is DataGridViewWithCheckBox. Add the dataGridView control and copy this code and paste.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceDataGridViewWithCheckBox
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load(objectsender, EventArgs e)
{
DataGridV.ColumnCount = 3;
DataGridV.Columns[0].Name = “Emp_code”;
DataGridV.Columns[1].Name = “Emp_Name”;
DataGridV.Columns[2].Name = “Emp_Salary”;
string[] rw = new string[] { “101”, “John”, “15000” };
DataGridV.Rows.Add(rw);
string[] rw1 = new string[] { “102”, “Fleming”, “10000” };
DataGridV.Rows.Add(rw1);
string[] rw2 = new string[] { “103”, “Steph”, “60000” };
DataGridV.Rows.Add(rw2);
string[] rw3 = new string[] { “104”, “Jay”, “50000” };
DataGridV.Rows.Add(rw3);
DataGridViewCheckBoxColumn chkbtn = new DataGridViewCheckBoxColumn();
// chkbtn.UseColumnTextForButtonValue = true;
DataGridV.Columns.Add(chkbtn);
}
privatevoid DataGridV_CellContentClick(object sender, DataGridViewCellEventArgse)
{
if(e.ColumnIndex == 3)
{
MessageBox.Show(DataGridV.Rows[e.RowIndex].Cells[“Emp_Name”].Value.ToString() + “ Selected”);
}
}
}