方法一:DataSource绑定数据源必须为:DataTable
第一种情况:
DataView dv = this.gridView1.DataSource as DataView;
DataTable dt= dv.Table;
DataRow[] rows= dt.Select(this.gridView1.RowFilter); // RowFilter-->为列表中的筛选条件
第二种情况:
BindingSource dv = this.gvwResult.DataSource as System.Windows.Forms.BindingSource;
DataTable dt = dv.DataSource as DataTable;
DataRow[] filteredRows = dt.Select(this.gvwResult.RowFilter);
方法二:使用GridControl自带的方法 -该方法通用
GridControl.DataController.GetAllFilteredAndSortedRows();
测试代码:
//获取到条件筛选以后的数据,判断筛选后的数据是否为空 var printData = gvDeductDetail.DataController.GetAllFilteredAndSortedRows(); if (printData.Count == 0) { DevMessageBox.ShowMsg("筛选数据不可为空!!!!"); return; } //由于不能直接将printData强制转换为List<TestModel>;所以循环数据添加到集合中 List<TestModel> listDetail = new List<TestModel>(); for (int i = 0;i<printData.Count;i++) { TestModel testModel = (TestModel)printData[i]; listDetail.Add(testModel); }