C# .NET - check/uncheck DataGridViewCheckbox on selected cell dynamically

Asked By VJ chaudhary on 10-May-12 07:53 AM
How to check/uncheck DataGridViewCheckbox cell on selected cell dynamically
[)ia6l0 iii replied to VJ chaudhary on 10-May-12 12:10 PM
I hope you want to check/uncheck a checkbox that is part of the DataGridViewCheckbox column. 

Then, you would write something like,
bool checked = (bool) ((currentRow.Cells[columnIndex] as DataGridViewCheckBoxCell).value);
if (checked)
{
//do something
}
else
{
//do something else.
}

Hope this helps.
Jitendra Faye replied to VJ chaudhary on 11-May-12 01:16 AM
Simply try this-

if( ((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true)
  {
    ((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = false

  }
esle
  {
    ((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true

  }


YOu can change index based on your columns.


TRy this and let me know.
Chintan Vaghela replied to VJ chaudhary on 11-May-12 02:00 AM

Hello,

Set your DataGridViewCheckBoxCell position as per Cells[0]

 

In followning DataGridViewCheckBoxCell is added first position.

 

Added Cell Click event as following way

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

      {

        DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];

        if ((bool)cell.Value == true)

        {

          cell.Value = "false";

        }

        else

        {

          cell.Value = "true";

        }

      }

 

 

 

Hope this helpful!

Thanks

 

 

 

VJ chaudhary replied to Jitendra Faye on 12-May-12 01:30 AM
OOps!....I'm not asking this.

It is Cell selection and i want to change cell value as true or false, which was selected, dynamically.
VJ chaudhary replied to Chintan Vaghela on 12-May-12 01:32 AM
OOps!....

That's not working b'coz it  will work on cell click event not dynamically as another button click event.
Chintan Vaghela replied to VJ chaudhary on 12-May-12 02:00 AM
where is you dynemicaly added control?
VJ chaudhary replied to [)ia6l0 iii on 12-May-12 02:16 AM
Oops!..... It is not working..

Actually I want do some task on button click event for those record which DataGridViewCheckBox was Checked.

And after performing task we should cleared of checkboxes. But the problem is which was last selected Cell.
On that Cell does  not work for check/Uncheck both cases.

Could any one resolve this and tell me why does it happen??????
VJ chaudhary replied to Chintan Vaghela on 12-May-12 02:18 AM
I',m not added control dynamically.
all these are predefined column collection as DataGridViewCheckBox.
Chintan Vaghela replied to VJ chaudhary on 12-May-12 03:03 AM
did you send me DataGridview design page?