Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Checkbox in DataGridView
Checkbox in DataGridView
Checkbox in DataGridView


by Awadhendra Tiwari on 8/11/2011 4:36:03 PM

Views: 13813       Comments: 0

Checkbox in DataGridView

In this article I will show you that how to check status of checkbox in datagridview that is it is checked or not. If checkbox in datagridview is checked then a message box display which have true and if checkbox in datagridview is unchecked then a message box display which have false value.

To complete this article drag and drop datagridview in your windows forms. Then on the load event of form write down following code to bind some values in datagrid view.

/// <summary>

        /// At the load event of form create a DataTable object

        /// and add two columns in that table. Data type of first

        /// column must be bool and second is string. When You

        /// bind the values of this datatable to datagrid then you

        /// see that that column whose data type is bool is converted

        /// into checkbox in datagridview and it have value true then

        /// it is checked and if it has false then it is unchecked.

        /// Below code explains that how to create DataTable object

        /// and how to bind that data in gridview.

        /// </summary>

        private void Form1_Load(object sender, EventArgs e)

        {

            DataTable dt = new DataTable();   //Creating data table object.

            dt.Columns.Add("Status", typeof(bool));   //Adding column in datatable and defining its datatype as a bool.

            dt.Columns.Add("Value", typeof(string));  //Adding another column in datatable.

 

            string[] data = { "AAA", "BBB", "CCC", "DDD", "EEE" };  //Creating an array of type string.

            bool[] status = { true, true, false, false, false };   //Creating an array of type bool.

            //Adding rows and value in DataTable.

            for (int i = 0; i < 5; i++)

            {

                DataRow dr = dt.NewRow(); 

                dr["Status"] = status[i];

                dr["Value"] = data[i];

                dt.Rows.Add(dr);

            }

            dataGridView1.DataSource = dt;  //Binding DataTable values in GridView.

        }

After binding values in DataGridView on the CellContentClick event of datagridview write down following code to display value of datagridview checkbox.

/// <summary>

        /// This event raised when we click on cell's content.

        /// Here I will show a message box containing true and

        /// false value when checkbox of datagridview is checked

        /// or unchecked accordingly.

        /// </summary>

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            //This condition will make sure that this code execute if and only if checkbox cell is clicked.

            if (e.ColumnIndex == dataGridView1.Columns["Status"].Index)  //Checking index of checkbox column is equal to clickable cell index.

            {  

                dataGridView1.EndEdit();  //Stop editing of cell.

                MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());  //Displaying value of that cell which is either true or false in this case.

            }

        }

Following snapshot will display result

When form is loaded the it display two columns in datagridview with some values.

Checkbox in DataGridView

When you click on checked checkbox then it will display a message box with true value and when you click on unchecked checkbox then it will display a message box with false value.

Checkbox in DataGridView

Checkbox in DataGridView

Thanks.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 3933
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.