Articles in this section

How to retrieve arbitrary selected ranges in WinForms GridControl?

This article explains how to retrieve an arbitrarily selected range in the WinForms GridControl.


Retrieve an arbitrary selected range

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 a 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.

GridRangeInfo range = new GridRangeInfo();
   range.ExpandRange(1, 1, this.gridControl1.RowCount, this.gridControl1.ColCount);
Dim range As New GridRangeInfo()
range.ExpandRange(1, 1, Me.gridControl1.RowCount, Me.gridControl1.ColCount)

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

Retrieving selected ranges

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

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));
  }
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

 

Conclusion

I hope you enjoyed learning about how to retrieve arbitrary selected ranges in WinForms GridControl.


You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.


For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.


If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

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