Subscribe For Free Updates!

We'll not spam mate! We promise.

Sep 29, 2011

how to add CheckBox inside DataGridView in windows application

Views:

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

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment

Become a Fan

visual studio learn