Home > DeveloperSection > Forums > How to add checkbox in datagridview in C#
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    June-15-2013 4:27 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 5810  View(s)
Rate this:
Hi Mindstickians,
i want to add a checkbox in datagriview in C#.

Can anybody provide me code for add checkbox in datagridview in C#.

Thanks in advance.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 7:49 AM

Hi Ankita,

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.

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;

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick