Articles in this section
Category / Section

How to retrieve arbitrary selected ranges in WinForms GridControl?

6 mins read

 

This article explains about how to retrieve arbitrary selected ranges in WinForms GridControl.

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

 

Conclusion

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

You can refer to our WinForms GridControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 WinForms GridControl and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-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)
Please sign in to leave a comment
Access denied
Access denied