I have a grid where every row has an un-bound check box column appended to it. I have disabled the check box when a simple condition (column in that row equals 1) exists on that row.This is done on the initial data binding.
But, some of these rows have children with a seperate, bound check box that, when checked, need to over-ride the condition that set its parent to be disabled (above). Any ideas on how this can be done? I have included a picture that may describe what I want a little better. I have completed the initial disabling, now I just need to deal with the ignores.
Hi,
Looping through all the rows in the grid initially is probably not the most efficient way togo. And also, I assume you want the checkbox to become enabled again when the column it's based on no longer equals 1.
So what I recommend is that you using the InitializeRow event instead. This event fires for each row initially and it will also fire when a cell value in the rows changes. So you can check inside InitializeRow if the value of the other column ir a one or if the child row's Ignore is checked.
The only problem with this is that the InitializeRow for the parent row won't fire when you check of uncheck the value on the child row. But this is easily handled. All you have to do is trap the CellChange event and watch for changes to the CheckBox cell in the child row. When it changes, you call grid.Rows.Refresh(FireInitializeRow).
I didn't follow your method completely, but I think this is a valid solution. Using the cell activation property works a little better for me because I have other methods that override things like the "Ctrl+A" key press to select all the rows. So instead of checking for a specific background color I can just look to see if the cells activation state.
private void grid_InitializeRow(object sender, InitializeRowEventArgs e){ if (e.Row.Band.Key == "ParentBand" && (int)e.Row.Cells["condition"].Value == 1 && (int)e.Row.Cells["init_done"].Value == 0) { // Disable Parent Checkbox Cell When Condition Exists e.Row.Cells["Select"].Activation = Activation.Disabled; e.Row.Cells["Select"].Value = false; // make sure checkbox is unchecked e.Row.Cells["init_done"].Value = 1; // Grid Initialization is finished, we don't want the second InitializeRow event to effect this column anymore } else if (e.Row.Band.Key == "ParentBand" && (int)e.Row.Cells["condition"].Value != 1 && (int)e.Row.Cells["init_done"].Value == 0) { // Enable Parent Checkbox Cell When Condition does not Exists e.Row.Cells["Select"].Activation = Activation.AllowEdit; e.Row.Cells["Select"].Value = true; // make sure checkbox is checked e.Row.Cells["init_done"].Value = 1; // Grid Initialization is finished }}private void grid_CellChange(object sender, CellEventArgs e) { // If the Ignore Checkbox is what was changed if (e.Cell.Column.Key == "Ignore") { object[ siblings = e.Cell.Row.ParentRow.ChildBands[0].Rows.All; // There may be multiple child rows int numIgnored = 0; int numSiblings = 0; foreach (object o in siblings) { UltraGridRow r = ((UltraGridRow)o); numSiblings++; if (r.Cells["Ignore"].Text == "True") numIgnored++; } // If the number you have ignored, isn't the same as the total number of siblings, don't let them check to create the parent if (numIgnored != numSiblings) { // disable the parent rows checkbox e.Cell.Row.ParentRow.Cells["Select"].Value = false; e.Cell.Row.ParentRow.Cells["Select"].Activation = Activation.Disabled; } else { // Enable the parent rows checkbox e.Cell.Row.ParentRow.Cells["Select"].Value = true; e.Cell.Row.ParentRow.Cells["Select"].Activation = Activation.AllowEdit; } } }