Tuesday, September 16, 2014

Selecting a row/record in Syncfusion's GridGroupingControl manually or at runtime

Here are following steps to select a row in gridgroupingcontrol manually:

1 - // Set  these properties
     //Inherited GridControlBase Selection

//turn on the GridGroupingControl's listbox mode (uses Table.SelectRecords collection)
            gridGroupingControl1.TableOptions.ListBoxSelectionMode = SelectionMode.One; 
//turn off GridControlBase selection support (do not use TableControl.Selections collection
      gridGroupingControl1.TableOptions.AllowSelection = ((Syncfusion.Windows.Forms.Grid.GridSelectionFlags)((Syncfusion.Windows.Forms.Grid.GridSelectionFlags.Row | Syncfusion.Windows.Forms.Grid.GridSelectionFlags.AlphaBlend)));
        //indicate how you want current cell to look/behave
            gridGroupingControl1.TableOptions.ListBoxSelectionCurrentCellOptions = GridListBoxSelectionCurrentCellOptions.HideCurrentCell;

2 - // Call this function on button click or on some other event
  private void SetGridRow()
        {
            gridGroupingControl1.Table.Records[0].SetCurrent();
            gridGroupingControl1.TableControl.CurrentCell.Activate(0, 0);
        }

3 - Implement this event and it will raise automatically due to step 2
        private void gridGroupingControl1_TableControlCurrentCellActivated(object sender, GridTableControlEventArgs e)
        {
            GridGroupingControl grid = sender as GridGroupingControl;

            if (grid.Table.CurrentRecord != null)
            {
                grid.Table.SelectedRecords.Clear();
                Record rec = grid.Table.CurrentRecord;
                if (!grid.Table.SelectedRecords.Contains(rec))
                    grid.Table.SelectedRecords.Add(rec);
            }            
        }