Hi
What are the best solution to use check box in a column to change the selected row(s) instead of using the row selector? I need to enable, or disable a form button base on the row.selected.count
I was supprised that it is much more difficult than I thought it would be.
Regards
Bryan
Hi Bryan,
There's no easy way to associate the selected state of a row with a CheckBox. Once a cell goes into edit mode in the grid (which is must do in order to check/uncheck a checkbox), any selection will be lost.So what you need to do here is simulate selection without using actual selection.
What I would do is keep my own List<UltraGridRow> of the "selected" rows. Every time the checkbox changes, you update the list.
Here's some sample code:
private List<UltraGridRow> selectedRows = new List<UltraGridRow>(); private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { UltraGridLayout layout = e.Layout; UltraGridOverride ov = layout.Override; UltraGridBand band = layout.Bands[0]; // Turn off row selection. ov.SelectTypeRow = SelectType.None; // Add an unbound CheckBox (coolean) column to the grid to handle selection. if (false == band.Columns.Exists("Selected")) { UltraGridColumn selectedColumn = band.Columns.Add("Selected"); selectedColumn.DataType = typeof(bool); selectedColumn.Header.VisiblePosition = 0; } // Created an appearance for the selected row(s). if (false == layout.Appearances.Exists("Selected")) { Infragistics.Win.Appearance selectedRowAppearance = layout.Appearances.Add("Selected"); selectedRowAppearance.BackColor = SystemColors.Highlight; selectedRowAppearance.ForeColor = SystemColors.HighlightText; } } private void ultraGrid1_CellChange(object sender, CellEventArgs e) { // Check for changes in the "Selected" column. if ("Selected" == e.Cell.Column.Key) { // If the checkbox is checked, add the row to our selected rows list. // If the checkbox is unchecked, remove it from the list. UltraGridRow row = e.Cell.Row; bool isSelected = bool.Parse(e.Cell.Text); if (isSelected) this.selectedRows.Add(row); else this.selectedRows.Remove(row); // Update the appearance of the row based on the state of the "Selected" checkbox. UpdateSelectedRowAppearance(row); } } private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) { // Update the appearance of the row based on the state of the "Selected" checkbox. UltraGridRow row = e.Row; UpdateSelectedRowAppearance(row); } // Updates the appearance of the row based on whether or not the "Selected" checkbox is checked. private static void UpdateSelectedRowAppearance(UltraGridRow row) { if (true == row.Cells.Exists("Selected")) { bool isSelected = bool.Parse(row.Cells["Selected"].Text); if (isSelected) row.Appearance = row.Band.Layout.Appearances["Selected"]; else row.Appearance = null; } }
Thank you Mike, it was helpful.