Articles in this section
Category / Section

How to synchronize the selection of multiple grids in WinForms GridControl?

2 mins read

Synchronize the selection of multiple grids

In WinForms GridControl, the Synchronization of multiple grids can be achieved by using the SelectionChanged event. In this event, the selection range of the source grids can be copied into the target grid to synchronize the grids. The TopRowChanged event and the LeftColChanged event can be used to synchronize the top row index and the left column index changes when the grid is scrolled. Refer to the sample for more details. The following code explains how the selection change in one grid affects the other grid.

C#

// Selection changed event triggered for GridControl1 and GridControl2 added in Form Load.
this.gridControl1.SelectionChanged += new GridSelectionChangedEventHandler(gridControl1_SelectionChanged);
this.gridControl2.SelectionChanged += new GridSelectionChangedEventHandler(gridControl2_SelectionChanged);
// Improve Performance on Large Selections [ TopRowChanged Event ].
this.gridControl1.TopRowChanged += new GridRowColIndexChangedEventHandler(gridControl1_TopRowChanged);
this.gridControl2.TopRowChanged += new GridRowColIndexChangedEventHandler(gridControl2_TopRowChanged);
void gridControl1_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
{
    Synchronize_SelectionChanged(gridControl2, gridControl1, e);
}
void gridControl2_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
{
    Synchronize_SelectionChanged(gridControl1, gridControl2, e);
}
# region Synchronize_SelectionChanged 
void Synchronize_SelectionChanged(GridControl targetGrid, GridControl sourceGrid, GridSelectionChangedEventArgs e)
{
 int sourceCount = sourceGrid.Selections.Ranges.Count;
 int targetCount = targetGrid.Selections.Ranges.Count;
 int maxCount = Math.Max(sourceCount, targetCount);
// Invalidate ranges that were changed.
 for (int n = 0; n < maxCount; n++)
 {
          GridRangeInfo newRange = n < sourceCount ? sourceGrid.Selections.Ranges[n] : GridRangeInfo.Empty;
    GridRangeInfo oldRange = n < targetCount ? targetGrid.Selections.Ranges[n] : GridRangeInfo.Empty;
          if (!oldRange.Equals(newRange))
    {
  Console.WriteLine("{0} - {1}", oldRange, newRange);
  targetGrid.InvalidateRange(oldRange);
  targetGrid.InvalidateRange(newRange);
    }
 }
// Update underlying data structure - does not cause invalidate / paint ...
 targetGrid.Selections.Ranges.Clear();
 GridRangeInfo[] ranges = new GridRangeInfo[sourceCount];
 sourceGrid.Selections.Ranges.CopyTo(ranges, 0);                
 targetGrid.Selections.Ranges.AddRange(ranges);
}
# endregion

VB

' Selection changed event triggered for GridControl1 and GridControl2 in Form Load.
AddHandler gridControl1.SelectionChanged, AddressOf gridControl1_SelectionChanged
AddHandler gridControl2.SelectionChanged, AddressOf gridControl2_SelectionChanged
' Improve Performance on Large Selections [ TopRowChanged Event ].
AddHandler gridControl1.TopRowChanged, AddressOf gridControl1_TopRowChanged
AddHandler gridControl2.TopRowChanged, AddressOf gridControl2_TopRowChanged
Private Sub gridControl1_SelectionChanged(ByVal sender As Object, ByVal e As GridSelectionChangedEventArgs)
 Synchronize_SelectionChanged(gridControl2, gridControl1, e)
End Sub
Private Sub gridControl2_SelectionChanged(ByVal sender As Object, ByVal e As GridSelectionChangedEventArgs)
       Synchronize_SelectionChanged(gridControl1, gridControl2, e)
End Sub
'"Synchronize_SelectionChanged "
Private Sub Synchronize_SelectionChanged(ByVal targetGrid As GridControl, ByVal sourceGrid As GridControl, ByVal e As GridSelectionChangedEventArgs)
 Dim sourceCount As Integer = sourceGrid.Selections.Ranges.Count
 Dim targetCount As Integer = targetGrid.Selections.Ranges.Count
 Dim maxCount As Integer = Math.Max(sourceCount, targetCount)
'Invalidate any ranges that were changed.
 For n As Integer = 0 To maxCount - 1
           Dim newRange As GridRangeInfo
     If n < sourceCount Then
  newRange = sourceGrid.Selections.Ranges(n)
     Else
  newRange = GridRangeInfo.Empty
     End If
     Dim oldRange As GridRangeInfo
     If n < targetCount Then
  oldRange = targetGrid.Selections.Ranges(n)
     Else
  oldRange = GridRangeInfo.Empty
     End If
     If Not oldRange.Equals(newRange) Then
         Console.WriteLine("{0} - {1}", oldRange, newRange)
  targetGrid.InvalidateRange(oldRange)
  targetGrid.InvalidateRange(newRange)
     End If
  Next n
'Update underlying data structure - does not cause invalidate / paint ...
 targetGrid.Selections.Ranges.Clear()
 Dim ranges(sourceCount - 1) As GridRangeInfo
 sourceGrid.Selections.Ranges.CopyTo(ranges, 0)
 targetGrid.Selections.Ranges.AddRange(ranges)
End Sub

Synchronizing selection in multiple grid

Figure 1: Synchronizing Selections in multiple GridControls

Samples:

C#: SynchronizationSample-C#

VB: SyncroniczationSample.VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied