从DataGridView获取符合过滤器和排序的DataTable [英] Get DataTable from DataGridView respecting filters and sorting
问题描述
我有一个 System.Windows.Forms.DataGridView
填充了数据。我使用的是这样的代码:
I have a System.Windows.Forms.DataGridView
filled with data. I'm using some code like this:
System.Data.DataTable dataTable1;
System.Windows.Forms.BindingSource bindingSource1;
System.Windows.Forms.DataGridView dataGridView1;
// (...)
bindingSource1.DataSource = dataTable1;
dataGridView1.DataSource = bindingSource1;
bindingSource1.Filter = "Some Filter Here";
我现在需要的是恢复 dataGridView1中的数据
。但这并不需要复杂。我的dataGridView1是只读的,因此我唯一需要的是尊重顺序和使用的过滤器,并将数据导出到 DataTable
。
What I need now is to recover the data that are in dataGridView1
. But it don't need to be complicated. my dataGridView1 is readonly so the only thing I need is respect the order and the filter used, and export the data to a DataTable
.
有人可以帮助我吗?
推荐答案
如果我对您的理解正确,则需要在 DataGridView
作为DataTable。
您可以使用 DataTable.DefaultView属性为此目的,其中包括所有过滤器。
If I understand you correctly, you need the filtered rows displaying in DataGridView
as DataTable.
You can make use of DataTable.DefaultView property for this purpose which includes all the filters.
尝试一下
BindingSource bs = (BindingSource)dataGridView1.DataSource;
DataTable table = (DataTable)bs.DataSource;
DataTable filtered = table.DefaultView.ToTable();// here we get filtered datatable
这篇关于从DataGridView获取符合过滤器和排序的DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!