C# 数据视图行过滤器值到数据表的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9408659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dataview rowfilter value to datatable convertion
提问by Fernando
I want to convert dataview rowfilter value to datatable. I have a dataset with value. Now i was filter the value using dataview. Now i want to convert dataview filter values to datatable.please help me to copy it........
我想将数据视图 rowfilter 值转换为数据表。我有一个有价值的数据集。现在我正在使用数据视图过滤值。现在我想将数据视图过滤器值转换为数据表。请帮我复制它........
My partial code is here:
我的部分代码在这里:
DataSet5TableAdapters.sp_getallempleaveTableAdapter TA = new DataSet5TableAdapters.sp_getallempleaveTableAdapter();
DataSet5.sp_getallempleaveDataTable DS = TA.GetData();
if (DS.Rows.Count > 0)
{
DataView datavw = new DataView();
datavw = DS.DefaultView;
datavw.RowFilter = "fldempid='" + txtempid.Text + "' and fldempname='" + txtempname.Text + "'";
if (datavw.Count > 0)
{
DT = datavw.Table; // i want to copy dataview row filter value to datatable
}
}
please help me...
请帮我...
采纳答案by Manjunath K Mayya
You can use
您可以使用
if (datavw.Count > 0)
{
DT = datavw.ToTable(); // This will copy dataview's RowFilterd values to datatable
}
回答by Sai Kalyan Kumar Akshinthala
You can use DateView.ToTable()for converting the filtered dataview in to a datatable.
您可以使用DateView.ToTable()将过滤后的数据视图转换为数据表。
DataTable DTb = new DataTable();
DTb = SortView.ToTable();
回答by Homer
The answer does not work for my situation because I have columns with expressions. DataView.ToTable()will only copy the values, not the expressions.
答案不适用于我的情况,因为我有带有表达式的列。DataView.ToTable()只会复制值,而不是表达式。
First I tried this:
首先我试过这个:
//clone the source table
DataTable filtered = dt.Clone();
//fill the clone with the filtered rows
foreach (DataRowView drv in dt.DefaultView)
{
filtered.Rows.Add(drv.Row.ItemArray);
}
dt = filtered;
but that solution was very slow, even for just 1000 rows.
但该解决方案非常慢,即使只有 1000 行。
The solution that worked for me is:
对我有用的解决方案是:
//create a DataTable from the filtered DataView
DataTable filtered = dt.DefaultView.ToTable();
//loop through the columns of the source table and copy the expression to the new table
foreach (DataColumn dc in dt.Columns)
{
if (dc.Expression != "")
{
filtered.Columns[dc.ColumnName].Expression = dc.Expression;
}
}
dt = filtered;
回答by Sumant Singh
The below code is for Row filter from Dataview and the result converted in DataTable
下面的代码是来自 Dataview 的 Row 过滤器和在 DataTable 中转换的结果
itemCondView.RowFilter = "DeliveryNumber = '1001'";
dataSet = new DataSet();
dataSet.Tables.Add(itemCondView.ToTable());

