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
2275
Selecting multiple rows in an UltraWinGrid.UltraGrid
posted

I have a strange problem that I am trying to work through, and I cannot seem to solve it.

Basically I have a grid filling with data and a single column of checkboxes inserted into position 0.  What I want to do is to have the user click on a checkbox to either select or deselect a row.  So far so good.  Now I want to allow the user to select multiple rows in the grid, and when the spacebar is tapped, the checkboxs will toggle.  This is where it gets fun.  I have Row Selectors set to Show; Row Selector Header Style set to Default Behavior; Row Selection set to Extended Select. 

 In code, I am trapping the spacebar in the KeyDown event.  Then for each row that is selected in the Selected.Rows collection I toggle the value in the cell thus:

private void ugSellingTitles_KeyDown( object sender, KeyEventArgs e )
        {
            // Trap spacebar
            if ( e.KeyData == Keys.Space )
            {
                foreach ( UltraGridRow rowsSelected in this.ugSellingTitles.Selected.Rows )
                {
                    if ( ( (bool)rowsSelected.Cells[ "SELECT" ].Value ) == false )
                    {
                        rowsSelected.Cells[ "SELECT" ].Value = true;
                    }
                    else
                    {
                        rowsSelected.Cells[ "SELECT" ].Value = false;
                    }
                }
            }
        }

When I run my app and display the grid, I get a column to the left of my checkboxes that displays a triangle to indicate selection, which is fine.  I can click on the extended selector column and hold down the shift key and click several rows down highlighting a bunch of rows.  Then I can hold the the ctrl key and select additional individual rows.

When I tap my spacebar the first time, all the rows that are highlighted have their checkboxes checked. 

When I tap the spacebar a second time, all of the rows that are highlighted have their checkboxes cleared except for the last row.  It's checkbox is still checked.

When I toggle the spacebar a third time, all of the highlighted rows now have their checkboxes selected again except for the last row, which is now cleared.

When I toggle the spacebar a fourth time, all of the checkboxes are cleared.

When I toggle the spacebar a fifth time, all the checkboxes are set, and if I toggle a sixth time, we are back to all checkboxes being cleared except for the last one which is still checked.

Am I taking the right approach here?  Any ideas about what I could be doing wrong?

Thanks.

Parents
  • 3707
    Verified Answer
    posted

    You forgot to set the event as handled for your spacebar processing. To fix that we do something like:

    private void ugSellingTitles_KeyDown( object sender, KeyEventArgs e )
            {
                // Trap spacebar
                if ( e.KeyData == Keys.Space )
                {
                    foreach ( UltraGridRow rowsSelected in this.ugSellingTitles.Selected.Rows )
                    {
                        if ( ( (bool)rowsSelected.Cells[ "SELECT" ].Value ) == false )
                        {
                            rowsSelected.Cells[ "SELECT" ].Value = true;
                        }
                        else
                        {
                            rowsSelected.Cells[ "SELECT" ].Value = false;
                        }
                    }
                    e.Handled = true;
                }
                else
                   e.Handled = false;
            }
Reply Children
No Data