Back to Devexpress

ColumnView.GetSelectedRows() Method

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-3479b145.md

latest15.0 KB
Original Source

ColumnView.GetSelectedRows() Method

Returns handles of the selected rows (or cards).

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
public virtual int[] GetSelectedRows()
vb
Public Overridable Function GetSelectedRows As Integer()

Returns

TypeDescription
Int32[]

An array that contains handles that correspond to selected rows.

|

Remarks

Use the ColumnView.SelectedRowsCount property to obtain the number of selected rows/cards.

The following example implements the GetSelectedDataRowsCount method that returns the total number of selected data rows. If the GridView is grouped, the GetSelectedDataRowsCount method checks whether handles correspond to data or group rows (group row handles have a negative value: -1, -2, etc.):

csharp
int GetSelectedDataRowsCount(GridView view)
{
    if (view.GroupCount == 0)
        return view.GetSelectedRows().Length;

    int selectedDataRowsCount = 0;
    foreach (int rowHandle in view.GetSelectedRows())
    {
        if (rowHandle >= 0)
            selectedDataRowsCount++;
    }
    return selectedDataRowsCount;
}
vb
Private Function GetSelectedDataRowsCount(ByVal view As GridView) As Integer
    If view.GroupCount = 0 Then
        Return view.GetSelectedRows().Length
    End If

    Dim selectedDataRowsCount As Integer = 0
    For Each rowHandle As Integer In view.GetSelectedRows()
        If rowHandle >= 0 Then
            selectedDataRowsCount += 1
        End If
    Next rowHandle
    Return selectedDataRowsCount
End Function

If multiple row selection is disabled (see ColumnViewOptionsSelection.MultiSelect), the GetSelectedRows method returns an array with a single item that corresponds to the focused row handle. You can also use the View’s FocusedRowHandle property to obtain the focused row handle.

Read to the following topic for additional information: Multiple Row and Cell Selection.

Note

In Master-Detail Mode , Pattern Detail Views do not contain data and these views are never displayed within the grid. The GetSelectedRows method must not be invoked for these Views.

The GetSelectedRows method can be used only with Views (master or clone detail views) that display real data within the grid. Use the following methods to access these Views:

Example

This example obtains selected rows and modifies their values in the “Discounted” column.

If data is sorted or filtered, changes made to one grid row can make other rows change their order and, as a result, their row handles. For this reason, you cannot access selected rows by their handles and process these rows right away. Instead, pass row handles retrieved by the ColumnView.GetSelectedRows method to the ColumnView.GetDataRow method. This allows you to access underlying data source objects (in this example, DataRows), save them to an array, and safely change their values afterwards.

Each row modification forces the Grid View to update itself. The example encloses the code within BaseView.BeginUpdate and BaseView.EndUpdate method calls to avoid excessive updates.

csharp
ArrayList rows = new ArrayList();

// Add the selected rows to the list.
Int32[] selectedRowHandles = gridView1.GetSelectedRows();
for (int i = 0; i < selectedRowHandles.Length; i++) {
    int selectedRowHandle = selectedRowHandles[i];
    if (selectedRowHandle >= 0)
        rows.Add(gridView1.GetDataRow(selectedRowHandle));
}
try {
    gridView1.BeginUpdate();
    for (int i = 0; i < rows.Count; i++) {
        DataRow row = rows[i] as DataRow;
        // Change the field value.
        row["Discontinued"] = true;
    }
}
finally {
    gridView1.EndUpdate();
}
vb
Dim Rows As New ArrayList()

' Add the selected rows to the list.
Dim selectedRowHandles As Int32() = GridView1.GetSelectedRows()
Dim I As Integer
For I = 0 To selectedRowHandles.Length - 1
    Dim selectedRowHandle As Int32 = selectedRowHandles(I)
    If (selectedRowHandle >= 0) Then
        Rows.Add(GridView1.GetDataRow(selectedRowHandle))
    End If
Next
Try
    GridView1.BeginUpdate()
    For I = 0 To Rows.Count - 1
        Dim Row As DataRow = CType(Rows(I), DataRow)
        ' Change the field value.
        Row("Discontinued") = True
    Next
Finally
    GridView1.EndUpdate()
End Try

Run Demo: Obtain Selected Rows

Example

The following code shows how to select rows that contain “Mexico” in the Country column and copy data from these rows.

csharp
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

ColumnView view = gridControl1.MainView as ColumnView;
GridColumn colCountry = view.Columns["Country"];
GridColumn colCompany = view.Columns["CompanyName"];
if (colCountry == null || colCompany == null) return;

// Enable multiple row selection mode.
view.OptionsSelection.MultiSelect = true;
view.ClearSelection();
int rowHandle = -1;
// Select rows that contain 'Mexico' in the Country column.
while (rowHandle != GridControl.InvalidRowHandle) {
    rowHandle = view.LocateByDisplayText(rowHandle + 1, colCountry, "Mexico");
    view.SelectRow(rowHandle);
}
int[] selectedRowHandles = view.GetSelectedRows();
if (selectedRowHandles.Length > 0) {
    // Move focus to the first selected row.
    view.FocusedRowHandle = selectedRowHandles[0];
    // Copy the selection to the clipboard
    view.CopyToClipboard();
    // Copy the selected company names to a Memo editor.
    memoEdit1.Text = "";
    for (int i = 0; i < selectedRowHandles.Length; i++)
        memoEdit1.Text += view.GetRowCellDisplayText(selectedRowHandles[i], colCompany) + "\r\n";
}
vb
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Base

Dim view As ColumnView = CType(GridControl1.MainView, ColumnView)
Dim colCountry As GridColumn = view.Columns("Country")
Dim colCompany As GridColumn = view.Columns("CompanyName")
If colCountry Is Nothing OrElse colCompany Is Nothing Then Return
'Enable multiple row selection mode.
view.OptionsSelection.MultiSelect = True
view.ClearSelection()
Dim rowHandle As Integer = -1
'Select rows that contain 'Mexico' in the Country column.
While rowHandle <> GridControl.InvalidRowHandle
    rowHandle = view.LocateByDisplayText(rowHandle + 1, colCountry, "Mexico")
    view.SelectRow(rowHandle)
End While

Dim selectedRowHandles As Integer() = view.GetSelectedRows()

If selectedRowHandles.Length > 0 Then
    'Move focus to the first selected row.
    view.FocusedRowHandle = selectedRowHandles(0)
    'Copy the selection to the clipboard
    view.CopyToClipboard()
    'Copy the selected company names to a Memo editor.
    MemoEdit1.Text = ""
    For i As Integer = 0 To selectedRowHandles.Length - 1
        MemoEdit1.Text += view.GetRowCellDisplayText(selectedRowHandles(i), colCompany) & vbCrLf
    Next
End If

The following code snippets (auto-collected from DevExpress Examples) contain references to the GetSelectedRows() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-gridlookupedit-multiple-item-selection/CS/Form1.cs#L52

csharp
void Properties_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e) {
    int[] selectedRows = popupGridView2.GetSelectedRows();
    List<string> values = new List<string>();

winforms-preserve-grid-state-on-refresh/CS/RefreshHelperClass.cs#L86

csharp
RowInfo rowInfo;
int[] selectionArray = view.GetSelectedRows();
if (selectionArray != null) // otherwise we have a single focused but not selected row

winforms-grid-copy-cells-in-biff8-format-using-excel-export-api/CS/gridCopyToClipboardExample/CopyToClipboardHelper.cs#L40

csharp
void ExportRows(IXlSheet sheet) {
    int[] selectedRows = this.view.GetSelectedRows();
    foreach (int gridRowHandle in selectedRows) {

winforms-scheduler-drag-drop-appointments-from-grid/CS/T179722/Form1.cs#L116

csharp
IDataObject GetDragData(GridView view) {
    int[] selection = view.GetSelectedRows();
    if (selection == null)

connect-winforms-grid-to-backend-using-middletier-server/CS/WinForms.Client/MainForm.cs#L141

csharp
private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e) {
    int[] selectedRowHandles = gridView.GetSelectedRows();
    if((selectedRowHandles.Length == 1) && (gridView.GetRow(selectedRowHandles[0]) is Employee employee)) {

winforms-gridlookupedit-multiple-item-selection/VB/Form1.vb#L46

vb
Private Sub Properties_QueryResultValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.QueryResultValueEventArgs)
    Dim selectedRows As Integer() = popupGridView2.GetSelectedRows()
    Dim values As List(Of String) = New List(Of String)()

winforms-preserve-grid-state-on-refresh/VB/RefreshHelperClass.vb#L94

vb
Dim rowInfo As RowInfo
Dim selectionArray As Integer() = view.GetSelectedRows()
If selectionArray IsNot Nothing Then ' otherwise we have a single focused but not selected row

winforms-grid-copy-cells-in-biff8-format-using-excel-export-api/VB/gridCopyToClipboardExample/CopyToClipboardHelper.vb#L42

vb
Private Sub ExportRows(ByVal sheet As IXlSheet)
    Dim selectedRows As Integer() = view.GetSelectedRows()
    For Each gridRowHandle As Integer In selectedRows

winforms-scheduler-drag-drop-appointments-from-grid/VB/T179722/Form1.vb#L102

vb
Private Function GetDragData(ByVal view As DevExpress.XtraGrid.Views.Grid.GridView) As IDataObject
    Dim selection As Integer() = view.GetSelectedRows()
    If selection Is Nothing Then Return Nothing

connect-winforms-grid-to-backend-using-middletier-server/VB/WinForms.Client/MainForm.vb#L100

vb
Private Sub bbiDelete_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim selectedRowHandles As Integer() = gridView.GetSelectedRows()
    Dim employee As Employee = Nothing

See Also

MultiSelect

DeleteSelectedRows()

SelectedRowsCount

SelectionChanged

CopyToClipboard()

Multiple Row and Cell Selection

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace