)
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 retrieve arbitrary selected ranges in WinForms GridControl?

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

 

Retrieve arbitrary selected ranges

Ranges can be of different types:

  • row ranges
  • column ranges
  • cell ranges
  • table ranges

When a range object is a row range, then its range.Top and range.Bottom properties are populated to reflect the top and bottom rows of the range but, its range.Left and range.Right are indeterminate at this point.

Similarly, column ranges have range.Left and range.Right defined but, not top and bottom. So, before you can arbitrarily start using the properties of a range, you must check its type to see what properties are valid.

Converting an arbitrary range to cell range

The only range type that has all its properties populated are cells. So, when you are given an arbitrary range, you must make sure that all of its properties are populated by using range.ExpandRange that forces all the properties to be defined.

 

C#

GridRangeInfo range = new GridRangeInfo();
   range.ExpandRange(1, 1, this.gridControl1.RowCount, this.gridControl1.ColCount);

 

VB

Dim range As New GridRangeInfo()
range.ExpandRange(1, 1, Me.gridControl1.RowCount, Me.gridControl1.ColCount)

After running the above codes, the range becomes a Cell Range.

Retrieving selected ranges

To iterate through the arbitrary selected ranges, you must use the Selections.GetSelectedrange in code as follows.

C#

private void buttonAdv1_Click(object sender, System.EventArgs e)
{
     GridRangeInfo range = new GridRangeInfo();
     range.ExpandRange(1, 1, this.gridControl1.RowCount, this.gridControl1.ColCount);
     GridRangeInfoList rangeList;
     int totalRanges = 0;
     string selectedRanges = "";
     if (this.gridControl1.Selections.GetSelectedRanges(out rangeList, false))
     {
        foreach (GridRangeInfo range1 in rangeList)
        {
           totalRanges++;
           string s = "";
           switch (range1.RangeType)
           {
                //Format For Cell Range
                case GridRangeInfoType.Cells:
                s = string.Format("R{0}C{1}:R{2}C{3}", range1.Top, range1.Left, range1.Bottom, range1.Right);
                break;
                //Format For Cols Range
                case GridRangeInfoType.Cols:
                s = string.Format("C{0}:C{1}", range1.Left, range1.Right);
                break;
                //Format For Row Range
                case GridRangeInfoType.Rows:
                s = string.Format("R{0}:R{1}", range1.Top, range1.Bottom);
                break;
                //Format For Table Range
                case GridRangeInfoType.Table:
                s = "Table";
                break;
                default:
                break;
           }
           selectedRanges += s + " ";
        }
     }
     MessageBox.Show(string.Format("Total Ranges: {0} Ranges: {1}", totalRanges, selectedRanges));
        }

 

VB

Private Sub buttonAdv1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonAdv1.Click
 Dim range As New GridRangeInfo()
 range.ExpandRange(1, 1, Me.gridControl1.RowCount, Me.gridControl1.ColCount)
 Dim rangeList As GridRangeInfoList
 Dim totalRanges As Integer = 0
 Dim selectedRanges As String = ""
 If Me.gridControl1.Selections.GetSelectedRanges (rangeList, False) Then
     For Each range1 As GridRangeInfo In rangeList
    totalRanges += 1
    Dim s As String = ""
    Select Case range1.RangeType
                  'Format For Cell Range
   Case GridRangeInfoType.Cells
        s = String.Format("R{0}C{1}:R{2}C{3}", range1.Top, range1.Left, range1.Bottom, range1.Right)
                  'Format For Cols Range
   Case GridRangeInfoType.Cols
        s = String.Format("C{0}:C{1}", range1.Left, range1.Right)
                  'Format For Row Range
   Case GridRangeInfoType.Rows
        s = String.Format("R{0}:R{1}", range1.Top, range1.Bottom)
                  'Format For Table Range
   Case GridRangeInfoType.Table
        s = "Table"
   Case Else
    End Select
    selectedRanges &= s & " "
     Next range1
 End If
 MessageBox.Show(String.Format("Total Ranges: {0} Ranges: {1}", totalRanges, selectedRanges))
End Sub

Samples:

C#: Retrieves SelectedRanges CS

VB: Retrieves SelectedRanges 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