Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends,

I am working on windows application. IN which I am using datagridview . There is also a checkboxcolumn in datagridview. My need is when I checked the checkboxcell there should be some control enabled and when I unchecked control should be disabled. I am using CellContentClick Event. Should I use something else.



//code

C#
private void dgHelpDesk_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {

           for (int i = 0; i < dgHelpDesk.RowCount; i++)
           {
               if (Convert.ToBoolean(this.dgHelpDesk.Rows[i].Cells["checkboxcolumn"].Value))
               {
                   cbAssigned.Enabled = true;
                   cbStatus.Enabled = true;
                   cbWorkFlow.Enabled = true;

               }

               else
               {
                   cbAssigned.Enabled = false;
                   cbStatus.Enabled = false;
                   cbWorkFlow.Enabled = false;
               }
           }
       }
Posted
Updated 20-Mar-13 4:21am
v4
Comments
Neetesh Agarwal 20-Mar-13 10:23am    
If you dont know answer then why you downvote.........
Prasad Khandekar 20-Mar-13 10:24am    
Neetesh please have a look at this doc (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx). It should help you.
Neetesh Agarwal 20-Mar-13 10:28am    
Thanks Prasad ji. I am finding this.
Neetesh Agarwal 20-Mar-13 10:52am    
Its working fine,,,,,,

1 solution

No, you rather need to get an instance of a CheckBox and handle the event CheckBox.CheckedChanged:
http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checkedchanged.aspx[^].

To get a control (to add an event handler to the invocation list of its event instance, as in your case): http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrol.aspx.

[EDIT]

Okay, as some delicate moment is involved, I'll show you what to do:
C#
void SetupGridViewEditEvents(DataGridView gridView) {
    gridView.CellValueChanged += (sender, eventArgs) => {
        DataGridViewCell cell = gridView.Rows[eventArgs.RowIndex].Cells[eventArgs.ColumnIndex];
        DataGridViewCheckBoxCell checkBoxCell = cell as DataGridViewCheckBoxCell;
        if (checkBoxCell != null) {
            CheckState state = (CheckState)checkBoxCell.Value;
            HandleCellCheckBoxCheckedChanged(
                eventArgs.RowIndex,
                eventArgs.ColumnIndex,
                state);
        } // if checkBoxCell
        //...
    }; // gridView.CellValueChanged
} // SetupGridViewEditEvents

void HandleCellCheckBoxCheckedChanged(int cellRow, int cellColumn, CheckState state) {
    // you handler logic goes here
} // HandleCellCheckBoxCheckedChanged


[EDIT]

Code sample fixed on 3/28/2013. Sorry for the inconvenience.

—SA
 
Share this answer
 
v6
Comments
Neetesh Agarwal 20-Mar-13 10:27am    
Hello Sir,

Thanks for reply But thats not the answer of my question. I am asking about checkbox in Gridview.
Sergey Alexandrovich Kryukov 20-Mar-13 10:28am    
You did not get it. I am answering about a check box in DataGridView. It contains a set of check boxes.
—SA
Sergey Alexandrovich Kryukov 20-Mar-13 10:32am    
Please see updated answer, after [EDIT].
—SA
Neetesh Agarwal 20-Mar-13 10:28am    
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx
Neetesh Agarwal 20-Mar-13 10:52am    
May be but my answer is here.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx
By the way thanks for ur time .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900