Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridViewCheckBox getting check/uncheck checkbox event
 

DataGridViewCheckBox getting check/uncheck checkbox event

Hi all!!

I am in trouble while I am trying to get the user' click in order to know ifthe userchecks or unchecks a checkbox

What is it wrong with the code below? Everytime that I check the checkbox, even if it turn checked or unchecked, the cell.Value is always 0. I also tried with other events like cellContentClick and it had the same behaviour.
        private void _collectorDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {
            DataGridViewCheckBoxCell cell; 
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                cell = (DataGridViewCheckBoxCell)_collectorDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell.Value == cell.TrueValue)
                {
                    //Checked - It never goes here
                    currentActivity.collectorList.Add((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                    notList.Remove((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                }
                else
                {
                    //Unchecked
                    currentActivity.collectorList.Remove((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                    notList.Add((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                }
            }
        }
Thank you in advance!
Gustavo
gborgesbr  Tuesday, September 22, 2009 4:35 PM

Hi gborgesbr,

The value of the cell would only be modified when the cell leaves. We can use the EditedFormattedValue property instead. This is the modified code snippet:

        private void _collectorDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {
            DataGridViewCheckBoxCell cell; 
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                cell = (DataGridViewCheckBoxCell)_collectorDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                //Change Value of Cell to EditedFormattedValue
                if (cell.EditedFormattedValue == cell.TrueValue)
                {
                    //Checked - It never goes here
                    currentActivity.collectorList.Add((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                    notList.Remove((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                }
                else
                {
                    //Unchecked
                    currentActivity.collectorList.Remove((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                    notList.Add((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                }
            }
        }

 

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 24, 2009 4:14 AM

Hi gborgesbr,

The value of the cell would only be modified when the cell leaves. We can use the EditedFormattedValue property instead. This is the modified code snippet:

        private void _collectorDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {
            DataGridViewCheckBoxCell cell; 
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                cell = (DataGridViewCheckBoxCell)_collectorDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                //Change Value of Cell to EditedFormattedValue
                if (cell.EditedFormattedValue == cell.TrueValue)
                {
                    //Checked - It never goes here
                    currentActivity.collectorList.Add((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                    notList.Remove((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                }
                else
                {
                    //Unchecked
                    currentActivity.collectorList.Remove((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                    notList.Add((collectorVo)_collectorDataGridView.Rows[e.RowIndex].Tag);
                }
            }
        }

 

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 24, 2009 4:14 AM

You can use google to search for other answers

Custom Search

More Threads

• How to zoomin and zoomout my picturebox.
• display cross mark instead of tick mark on checkbox
• System.IO.FileNotFoundException In Windows Application
• Good UI Pattern
• Rendering Aero Glass to a WinForms form?
• How to declare single Object for all forms...?
• Custom Draw ListView
• Make setup program create folders?
• Listview control exception
• Button text and tag from a database row