RSS

Add CheckBox inside DataGridView in windows application

16 Nov

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.

  /// <summary>

        /// Create DataTable to add to DataGridView

        /// </summary>

        /// <returns>DataTabe</returns>

        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!!!

praveen_7108_CheckBox_GridView.zip

Referenced by:  http://www.dotnetspark.com/kb/151-add-checkbox-inside-datagridview-windows.aspx

Advertisements
 
Leave a comment

Posted by on November 16, 2010 in C#, Window Application

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: