How to: Get Data Rows in a Filtered Grid
- 2 minutes to read
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; }
}
}
See Also