.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Add CheckBox inside DataGridView in windows application

Posted By:Praveen       Posted Date: November 29, 2008    Points: 25    Category: C#    URL:   
 

This article will explain you how to insert Checkbox inside DataGridView in windows application. This article is for beginners.

First create a windows application from Visual Studio Menu File->Project-> Then select C#->Windows Form Application.

Then add DataGridView from toolbox of Visual Studio. And also add One Button to test the CheckBox Value inside DatGridView.

Now from the property window of DataGridView Select the Columns Property.
You will see this screen:


 
Click on the (Collection) button from the above shown window.
You will see the below screen:


From this window click on "Add" button.
You will see below window:


 
From the above window select "Type" as "GridViewCheckboxColumn" Column as shown in above window
Then press Add Button from above Window.
Now you have added the checkbox DataGridView in windows application. Now it's time to add some code in your .cs file .

Now create one function, which will return a DataTable with sample data containing in it. This Data Table  will bind to DataGridView.

  ///

        /// Create DataTable to add to DataGridView

        ///

        /// DataTabe

        private DataTable SampleDataTable()

        {

            DataTable dt = new DataTable("MyDataTable");

 

            //Create columns and add to DataTable;

 

            DataColumn dcID = new DataColumn("ID");

            dt.Columns.Add(dcID); //ID column created and add to DataTable

 

            DataColumn dcSomeText = new DataColumn("SomeText");

            dt.Columns.Add(dcSomeText); //LastName column created and add to DataTable

 

            //Now Add some data to the DataTable

            DataRow dr;

            for (int count = 0; count <= 9; count++)

            {

                dr = dt.NewRow();

                dr["ID"] = count;

                dr["SomeText"] = "Some Text " + count;

                dt.Rows.Add(dr);

            }

 

            return dt;

 

        }


On the FormLoad_Event add this line of code to bind SampleDataTable() as a datasource of of windows application like this

private void Form1_Load(object sender, EventArgs e)

{

      myDataGridView.DataSource = SampleDataTable();

 

}


Now on Button click Event add this line of code to check what all rows user has selected from DataGridView.

private void btnSubmit_Click(object sender, EventArgs e)

{

     foreach (DataGridViewRow dr in myDataGridView.Rows)

     {

         if(dr.Cells[0].Value != null) //Cells[0] Because in cell 0th cell we have added checkbox

          {

                    MessageBox.Show("Rows " +dr.Index + " selected");

          }

      }

}

Now, its time to run the application,once you run your application you will see the output window like this:

Now, select the check box and press the Get Selected Rows Button. You will see this window:

Happy Coding!!!


 Subscribe to Articles

     

Further Readings:

Responses
Author: Sanjay Kumar         Company URL: http://www.devil2devil.blogspot.com
Posted Date: April 16, 2009

Hi Praveen,
Thanks for the reply but dats not my reqyirement, what you have given is too basic I need checkbox at header too... plz read my requirement once again.
Regards,

Hi I want to use checkboxes with headercheckbox in datagridview so that user can select/deselect all records at single click. Also user should be able to randomly select records. If not all records are selected then headercheckbox should remain unchecked. If all records are checked individually then the headercheckbox should get checked automatically.in .net window application
with C#.
Author: Admir         Company URL: http://www.dotnetspark.com
Posted Date: May 08, 2010

Thank you on your article, it helped me a lot.
Only one thing that I do not understand , How I can remember Check Box Selection, after user do a sorting of datagridview. Now your example working well but in run time if You select some check box and after that Do resorting, Datagridview clear that selection. Is there a way to datagridview has selected check box and after user do sorting.
Thank you in advance
Author: shahab         Company URL: http://www.dotnetspark.com
Posted Date: August 18, 2010

hello dear friend,

on this line :
"if(dr.Cells[0].Value != null)"
the "value" can be "!= null" and be equal to "false"
so this row is not selected but is shown as a selected row on this line:
"MessageBox.Show("Rows " +dr.Index + " selected");"

Thanks anyway,


shahab
Author: iffee         Company URL: http://www.dotnetspark.com
Posted Date: April 01, 2011

Hello friend,

I have a problem here. First time its OK. but when i change my check box selection, dr get old selection as well. Please look into and give me resolution.
Author: dennis         Company URL: http://www.dotnetspark.com
Posted Date: April 13, 2011

THANK YOU SIR!!!
very well written & concise ...

you definitely saved me some time & frustration!
it works great!

MUCH APPRICAITED
Author: iffee         Company URL: http://www.dotnetspark.com
Posted Date: April 13, 2011

if (dr.Cells[0].Value != null)
{
add the following line for chk box selection.
if ((bool)dr.Cells[0].Value == true)
{
// your code

}
}

Author: MemtechLodhi         Company URL: http://www.mindstick.com
Posted Date: September 19, 2011

Hello Everyone,
Great article!!!!! thanks for sharing with us..
for more details on checkbox in datagridview using c#, you may check out the following links.....
http://mindstick.com/Articles/77b73daa-2307-4de6-91df-7d6fe1094731/?Checkbox%20in%20DataGridView

Thanks!!!!
Author: Bharath         Company URL: http://www.dotnetspark.com
Posted Date: March 13, 2012

Hi,

This article was of a great help to me. But there is one problem here. There will be a empty last row in the grid and if it is selected a new empty row is getting added. Please let me know how to stop this. Requesting for your early response.

Thanks,
Bharath

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend