Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2715
Cancel checkbox click
posted

Hi !

I have a grid with a checkbox column. I add the checkbox column like this :

valuesGrid.DisplayLayout.Bands[0].Columns.Add("Select");

valuesGrid.DisplayLayout.Bands[0].Columns["Select"].DataType = typeof(Boolean);

I need to have only one checkbox checked at all times - the user should uncheck the previously checked one, before checking a new one. I've used the cellClick event to check if other checkboxes are checked, but when (if true) i try to uncheck the clicked one, it doesn't quite work. I'm unchecking it like this : 

e.Cell.Row.Cells[10].Value = false;

Any ideeas what am i doing wrong ?

  • 469350
    Verified Answer
    Offline posted

    Hi,

    What exactly does not work?

    I would have expected you to uncheck the previously-checked cell and allow the new check to essentially replace it as the checked item. But from what you have here, you seem to be unchecking the cell the user just clicked on. Is that what you want?You want to force the user to explicitly uncheck the previously checked item before checking a new item rather than just replacing the old checked item with the new one? That seems like an unusual UI.

    But if that's what you want, then you probably cannot use the CellClick event. Or at least not directly - because this is probably an issue of timing. The cell is in the middle of the process of being checked when you are trying to uncheck it. If you used a BeginInvoke to set the cell value instead of setting it directly inside the event, it would probably work.

    But it seems to me that it would be cleaner and more efficient for you to disable all of the cells in the column except the checked one. You would have to store the checked cell and then use InitializeRow to determine the enabled state of the cells in the column.

     


            UltraGridCell checkedCell = null;
            private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e)
            {
                UltraGridCell checkBoxCell = e.Row.Cells["Boolean 1"];
                if (this.checkedCell == null || checkBoxCell == this.checkedCell)
                    checkBoxCell.Activation = Activation.AllowEdit;
                else
                    checkBoxCell.Activation = Activation.Disabled;           
            }

            private void ultraGrid1_CellChange(object sender, CellEventArgs e)
            {
                if (e.Cell.Column.Key == "Boolean 1")
                {
                    if (e.Cell.Text == true.ToString())
                        this.checkedCell = e.Cell;
                    else
                        this.checkedCell = null;

                    UltraGrid grid = (UltraGrid)sender;
                    grid.Rows.Refresh(RefreshRow.FireInitializeRow);
                }
            }