Add CheckBox in DataGridView in C# windows application.

Bageshkumarbagi
Posted by Bageshkumarbagi under C# category on | Points: 40 | Views : 18330
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewCheckBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btn_load_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Select", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Employee No");
dt.Columns.Add("Employee Name");
dt.Columns.Add("Join Date");

DataRow dr;

for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr["Select"] = false;
dr["Employee No"] = 1000 + i;
dr["Employee Name"] = "Employee " + i;
dr["Join Date"] = DateTime.Now.ToString("dd/MM/yyyy");

dt.Rows.Add(dr);
}

dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

dataGridView1.DataSource = dt;
}

private void btn_cancel_Click(object sender, EventArgs e)
{
this.Close();
}


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["Select"].Index)
{
dataGridView1.EndEdit();
if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Select"].Value)
MessageBox.Show("The Value is Checked","Status",MessageBoxButtons.OK,MessageBoxIcon.Information);
else
MessageBox.Show("The Value is UnChecked", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}

Comments or Responses

Login to post response