Click here to Skip to main content
11,704,044 members (69,627 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
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

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 20-Mar-13 3:44am
Edited 20-Mar-13 4:21am
v4
Comments
Neetesh Agarwal at 20-Mar-13 10:23am
   
If you dont know answer then why you downvote.........
Prasad Khandekar at 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 at 20-Mar-13 10:28am
   
Thanks Prasad ji. I am finding this.
Neetesh Agarwal at 20-Mar-13 10:52am
   
Its working fine,,,,,,

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  
v6
Comments
Neetesh Agarwal at 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 at 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 at 20-Mar-13 10:32am
   
Please see updated answer, after [EDIT].
—SA
Neetesh Agarwal at 20-Mar-13 10:28am
   
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx
Neetesh Agarwal at 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 .
Sergey Alexandrovich Kryukov at 20-Mar-13 11:13am
   
No, this is a bad, pull approach, you need a push.
—SA
Sergey Alexandrovich Kryukov at 20-Mar-13 11:25am
   
You should not work around and shift focus. You really need to handle CheckBox event, and nothing else.
Please see my updated answer, after [EDIT].

Seriously, don't do wrong thing again.
—SA
Neetesh Agarwal at 21-Mar-13 3:11am
   
Thanks Sir,
I will sure go through that......
Sergey Alexandrovich Kryukov at 21-Mar-13 10:31am
   
Great.
Good luck, call again.
—SA
Maciej Los at 28-Mar-13 12:41pm
   
+5
Sergey Alexandrovich Kryukov at 28-Mar-13 13:13pm
   
Thank you, Maciej.
—SA

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

  Print Answers RSS
0 Maciej Los 530
1 OriginalGriff 510
2 Sergey Alexandrovich Kryukov 421
3 Andy Lanng 294
4 F-ES Sitecore 220
0 OriginalGriff 8,744
1 Sergey Alexandrovich Kryukov 7,747
2 CPallini 5,107
3 Maciej Los 4,716
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100