Multiple Checkbox Editor

Dec 31, 2009 at 11:29 AM

Version 4.22

I have a grid using multiple checkboxes for each row of grid (which represent days of week)

I am trying to catch when user checks/unchecks box so I can update DB.

After a bit of searching for checkbox editor I came across example in Sample 03 using an InvertedDisabledCheckBox event handler

I have amended slightly so that it should pass in row and col as below. However the event handler is getting fired so that

a single checkbox click means the event is triggered for all checkboxes in the grid. This would be OK if I didnt have to make a db call

which I obviously want to limit to the grid cell that changed if possible. I have also tried just registering a single handler (against first checkbox cell)

but this still fires as many times as there are checkboxes in grid

Any pointers appreciated.

 

for (int i = 0; i < 7; i++)
{
    gridMain[r, i + 2] = new SourceGrid.Cells.CheckBox(null, true);
    gridMain[r, i + 2].FindController<SourceGrid.Cells.Controllers.CheckBox>().CheckedChanged += InvertDisabledCheckBox(r,i +2);
}


...
private EventHandler InvertDisabledCheckBox(int row, int col)
{
    return delegate
    {
         log.Debug("Checkbox row:" + row + " col:" + col);
    };
}


Dec 26, 2011 at 3:41 PM
Edited Dec 26, 2011 at 3:43 PM

Hi.

This also happens in SourceGrid 4.30!

What you have to do in this case is remove the standard checkbox controller and add a new new one, setting the parameter as 'false'. You have to do this before setting the CheckedChanged event handler.

This has to be done because there is no setter property defined, only a getter.

For example, the new code would look as the following:

 

grid [ _row, _line ].Controller.RemoveController ( grid [ _row, _line ].FindController<SourceGrid.Cells.Controllers.CheckBox> ( ) );
grid [ _row, _line ].Controller.AddController ( new SourceGrid.Cells.Controllers.CheckBox ( false ) );
grid [ _row, _line ].FindController<SourceGrid.Cells.Controllers.CheckBox> ( ).CheckedChanged += new EventHandler ( MyForm_CheckedChanged );

Hope this helps.

Merry Christmas :-)

Jul 16, 2015 at 9:44 PM
Edited Jul 16, 2015 at 10:31 PM
Zemira,

Thanks for getting me started on resolving this issue. I understand how the Button.OnClick event works, but the CheckBox.CheckedChanged event does seem awkward. My solution was to modify the SourceGrid\Cells\Controllers\CheckBox.cs file as follows:

Added:
/// <summary>
/// An event MORE similar to Windows.Forms.CheckBox.CheckedChanged.
/// Handled just like Button.OnClick, with sender argument containing CellContext
/// Fired when value changed
/// </summary>
public event EventHandler CheckedChangedSenderCellContext;

protected virtual void OnCheckedChangedSenderCellContext(object sender, EventArgs e)
{
    if (CheckedChangedSenderCellContext != null)
    {
        CheckedChangedSenderCellContext(sender, e);
    }
}
Added the SECOND line shown (below existing line) in UIChangeChecked:
OnCheckedChanged(EventArgs.Empty);
OnCheckedChangedSenderCellContext(sender, e);
Shown below is the modified frmSample03.cs. First is from the OnLoad handler:
//Boolean (CheckBox)
grid[currentRow, 0] = new SourceGrid.Cells.Cell("Boolean (CheckBox)");
grid[currentRow, 0].View = captionModel;
grid[currentRow, 1] = new SourceGrid.Cells.CheckBox(null, true);
SourceGrid.Cells.Controllers.CheckBox checkBoxClickEvent = new SourceGrid.Cells.Controllers.CheckBox();
checkBoxClickEvent.CheckedChangedSenderCellContext += new EventHandler(InvertDisabledCheckBox);
grid[currentRow, 1].Controller.RemoveController(grid[currentRow, 1].FindController<SourceGrid.Cells.Controllers.CheckBox>());
grid[currentRow, 1].Controller.AddController(checkBoxClickEvent);
And last, the new InvertDisabledCheckBox:
private void InvertDisabledCheckBox(object sender, EventArgs e)
{
    SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
    SourceGrid.Grid g = (SourceGrid.Grid)context.Grid;
    if (g != grid)
        return;

    int row = context.Position.Row;
    int col = context.Position.Column;
    g[row, 2].Value = !(((bool)g[row, 2].Value));
}
Note that InvertDisabledCheckBox verifies that the grid in the context is the one you want. This allows use of multiple grids.

I plan on submitting these changes to the project.

Cal