windowsforms-403848-controls-and-libraries-data-grid-examples-filtering-how-to-get-data-rows-in-a-filtered-grid.md
This example demonstrates how to obtain data rows currently displayed within a grid control and display them within another grid control. A common case is when you have filtered the grid and want to get rows that match a filter condition.
In this example, the GetDataRows method returns the list of data rows displayed within the grid. If rows are grouped, the list includes visible data rows and data rows hidden within collapsed group rows.
Play the following animation to see the result:
using System;
using DevExpress.XtraEditors;
using System.Collections.Generic;
using DevExpress.XtraGrid.Views.Base;
namespace DXApplication8 {
public partial class Form1 : XtraForm {
public Form1() {
InitializeComponent();
gridControl1.DataSource = new List<Person>() {
new Person(){Name = "Mike", Age = 23, Gender = "Male"},
new Person(){Name = "Ann", Age = 22, Gender = "Female"},
new Person(){Name = "John", Age = 30, Gender = "Male"}
};
simpleButton1.Click += SimpleButton1_Click;
}
private void SimpleButton1_Click(object sender, EventArgs e) {
gridControl2.DataSource = GetDataRows(gridView1);
}
List<Person> GetDataRows(ColumnView view) {
if (view == null) return null;
List<Person> rowList = new List<Person>();
for (int i = 0; i < view.DataRowCount; i++)
rowList.Add(gridView1.GetRow(i) as Person);
return rowList;
}
}
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}
}
Imports System
Imports DevExpress.XtraEditors
Imports System.Collections.Generic
Imports DevExpress.XtraGrid.Views.Base
Namespace DXApplication8
Partial Public Class Form1
Inherits XtraForm
Public Sub New()
InitializeComponent()
gridControl1.DataSource = New List(Of Person)() From {
New Person() With {
.Name = "Mike",
.Age = 23,
.Gender = "Male"
},
New Person() With {
.Name = "Ann",
.Age = 22,
.Gender = "Female"
},
New Person() With {
.Name = "John",
.Age = 30,
.Gender = "Male"
}
}
AddHandler simpleButton1.Click, AddressOf SimpleButton1_Click
End Sub
Private Sub SimpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
gridControl2.DataSource = GetDataRows(gridView1)
End Sub
Private Function GetDataRows(ByVal view As ColumnView) As List(Of Person)
If view Is Nothing Then
Return Nothing
End If
Dim rowList As New List(Of Person)()
For i As Integer = 0 To view.DataRowCount - 1
rowList.Add(TryCast(gridView1.GetRow(i), Person))
Next i
Return rowList
End Function
End Class
Public Class Person
Public Property Name() As String
Public Property Age() As Integer
Public Property Gender() As String
End Class
End Namespace
See Also