GDBG and a column of mutually exclusive checkboxes

I have a WinForm grid with one of the columns of Checkbox type.
How can I make sure that only one checkbox can be in the Checked state at any given time ?

I've tried the code below

private void gridDataBoundGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
GridCurrentCell cc = gridDataBoundGrid1.CurrentCell;
if ( (bool)cc.Renderer.StyleInfo.CellValue == false )
return;

for(int i = 1; i <= gridDataBoundGrid1.Model.RowCount; i++)
{

if ( i != cc.RowIndex )
gridDataBoundGrid1.Model[i, cc.ColIndex].CellValue = false;
}
}

but that resets all checkboxes to false.

Can anyone advise an alternative solution or why my code above is not working.
Your help is greatly appreciated.

2 Replies

AD Administrator Syncfusion Team October 19, 2006 09:56 AM UTC

Hi Albert,

Please try the code below in the CurrentCellChanged event handler of the grid, to have mutually exclusive checkboxes in a checkbox column.

private void gridDataBoundGrid1_CurrentCellChanged(object sender, EventArgs e)
{
GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
if(cc.Renderer.StyleInfo.CellType == "CheckBox")
{
bool check = (bool)cc.Renderer.StyleInfo.CellValue;
for(int i = 1; i <= this.gridDataBoundGrid1.Model.RowCount; i++)
{
if(check)
this.gridDataBoundGrid1.Model[i, cc.ColIndex].CellValue = false;
}
this.gridDataBoundGrid1.Model[cc.RowIndex, cc.ColIndex].CellValue = true;
}
}

Let me know if this helps.
Regards,
Rajagopal


AL Albert October 20, 2006 02:01 AM UTC

Thanks, Rajagopal.

It works apart from one of the requirements that I forgot to mention in my first post. It is allowed to have all checkboxes in the column to be unchecked, but only one of them can be checked.

Regards,
Albert

>Hi Albert,

Please try the code below in the CurrentCellChanged event handler of the grid, to have mutually exclusive checkboxes in a checkbox column.

private void gridDataBoundGrid1_CurrentCellChanged(object sender, EventArgs e)
{
GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
if(cc.Renderer.StyleInfo.CellType == "CheckBox")
{
bool check = (bool)cc.Renderer.StyleInfo.CellValue;
for(int i = 1; i <= this.gridDataBoundGrid1.Model.RowCount; i++)
{
if(check)
this.gridDataBoundGrid1.Model[i, cc.ColIndex].CellValue = false;
}
this.gridDataBoundGrid1.Model[cc.RowIndex, cc.ColIndex].CellValue = true;
}
}

Let me know if this helps.
Regards,
Rajagopal

Loader.
Up arrow icon