)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 19, 2019
Tags: selection

Synchronize the selection of multiple grids

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon