Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / Checkbox column to selected multiple rows.

Checkbox column to selected multiple rows.

New Discussion
Shika Goud
Shika Goud asked on Mar 15, 2018 12:58 AM

I have a column of checkboxes in my UltraGrid, where I can check more than one checkbox and I want to get all the rows in the UltraGrid that are checked. So far, I got to the point where I am able to click more than one checkbox but can only get the active cell/Row and not all the rows that are selected using checkbox.

Sign In to post a reply

Replies

  • 0
    Andrew Goldenbaum
    Andrew Goldenbaum answered on Mar 13, 2018 4:17 PM

    Hello Shika,

    I have been investigating into this requirement that you have to get all of the rows in your UltraGrid that are checked, although I'm not completely sure I understand your scenario in this case. It sounds like you are using a checkbox that is tied in with the UltraGrid's row selection, although I'm not completely sure.

    If this is the case, and every row that is selected is "checked," I would then recommend using the UltraGrid.Selected.Rows collection as this will return all of the rows that are currently selected in your grid.

    Otherwise, I would imagine that your checkboxes are somehow tied to your underlying data item. If this is the case, I would then recommend that you loop through the UltraGrid.Rows collection and get the ListObject off of each row. This will return your underlying data item, which would allow you to check the property your checkbox is "tied" to. This would allow you to see which rows are checked and which one's aren't.

    I hope this helps. Please let me know if you have any other questions or concerns on this matter.

    • 0
      Shika Goud
      Shika Goud answered on Mar 13, 2018 9:23 PM

      The Checkbox column is unbound, how can I tie to the data item or the row selection? I do not see anything in UltraGrid.Selected.Rows (count is 0). I don't think it is tied to either of these.
       
      (I am very new to this infragistics controls, please excuse me if any of this sounds silly)

      • 0
        Andrew Goldenbaum
        Andrew Goldenbaum answered on Mar 13, 2018 10:29 PM

        Hello Shika,

        If the column is unbound, I would expect you could then loop through the UltraGrid.Rows collection and get the cell that corresponds to your "checkbox" column and check its Value property. The code for this could look like the following, where "index" can be the numeric index or string key of the column that the cell belongs to, or the column itself:

        foreach(var x in grid.Rows)
        {
           var y = x.Cells[index].Value;
        }

        As for tying this to your underlying data item – if you have a bool property on your underlying data item, the UltraGrid will use a checkbox as the default "editor" for the column that would represent that bool property. As such, you would be able to loop through and use either the Value method above or the ListObject mentioned originally.

        Please let me know if you have any other questions or concerns on this matter.

      • 0
        Mike Saltzman
        Mike Saltzman answered on Mar 14, 2018 1:37 PM

        Just to clarify – the rows in this case are not "selected." Selected refers to the grid's built-in highlighting of rows and there's no connection between that and any individual checkbox column your grid happens to have. That's why the grid.Selected.Rows collection is empty. What you want are the rows that are checked, not selected.

        The code Andrew posted above will work in a very simple case, but there are a few caveats. If the users checks the checkbox and the grid and cell they checked still have focus, the Value property of that cell will not be updated, yet. Also, looping through the grid.Rows collection won't work if the grid rows are grouped is using OutlookGroupBy. That code also does not account for filtering presumably you don't want to count rows that are filtered out as checked. 

        Here's a routine that should work in all of those cases, and is also a bit more efficient, since it does not force the creation of the UltraGridCell objects. 

                private List GetCheckedRows(UltraGrid grid, string checkBoxColumnKey)
                {            
                    // Build a list of checked rows. 
                    var checkedRows = new List();
        
                    // Get all of the data rows (ignoring grouping) that arenot filtered out. 
                    var allDataRows = grid.Rows.GetFilteredInNonGroupByRows();
        
                    // If a cell is in edit mode, then the Value property will returning the underlying
                    // value and will not reflect pending changes on the screen that have not yet
                    // been committed. So see if there's an ActiveCell in edit mode in the checkbox
                    // column, because if there is, we will need to handle that differently. 
                    var checkBoxColumn = grid.DisplayLayout.Bands[0].Columns[checkBoxColumnKey];
                    var activeCell = grid.ActiveCell;
                    bool hasCellInEditMode = null != activeCell && 
                        0 == string.Compare(activeCell.Column.Key, checkBoxColumnKey, false)&&
                        activeCell.IsInEditMode;
        
                    foreach (var row in allDataRows)
                    {
                        bool isRowChecked;
        
                        // If there's an active cell in edit mode in the checkbox column
                        // we need to get it's checked state from the text, not the value, since
                        // the value is still pending committment. 
                        if (hasCellInEditMode &&
                            activeCell.Row == row)
                        {
                            // Get the text of the cell and parse it to a bool. 
                            // Also, use GetCellText instead of row.Cells["checkBoxColumn"].Text
                            // because this is more efficient and doesn't force the creation 
                            // of the UltraGridCell object. 
                            isRowChecked = bool.Parse(row.GetCellText(checkBoxColumn));
                        }
                        else
                        {
                            // GetCellValue instead of row.Cells["checkBoxColumn"].Value
                            // because this is more efficient and doesn't force the creation 
                            // of the UltraGridCell object. 
                            isRowChecked = (bool)row.GetCellValue(checkBoxColumnKey);
                        }
        
                        // If this row is checked, add it to the list. 
                        if (isRowChecked)
                            checkedRows.Add(row);
                    }
        
        
                    // Return the list of all checked rows. 
                    return checkedRows;
                }

        BTW… while this approach will work and is pretty efficient, it still might take a while to loop through all of the rows. And this approach forces the creation of every row in the grid, so it could take longer the first time you do it. These should only be an issue if you have a tremendous number of rows in your grid. 

        If that's the case, an alternative approach would be to trap for when the rows are checked and maintain your own list as you go. that way, you never have to loop through all the rows in the grid. But that approach is a bit more complex and, as I said, only makes sense if you have a very large number of rows. 

      • 0
        Shika Goud
        Shika Goud answered on Mar 15, 2018 12:58 AM

        Thanks Mike, exactly what I was looking for. And no, the rows are not many. So this works.

      • 0
        Shika Goud
        Shika Goud answered on Mar 15, 2018 12:49 AM

        Thanks Andrew for the direction.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Shika Goud
Favorites
0
Replies
6
Created On
Mar 15, 2018
Last Post
7 years, 11 months ago

Suggested Discussions

Created by

Created on

Mar 15, 2018 12:58 AM

Last activity on

Feb 11, 2026 8:45 PM