.net 循环遍历 DataView 中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1427840/
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
Looping through rows in a DataView
提问by Alex Angas
The DataViewobject doesn't have a Rowsproperty like DataTable.
该DataView对象没有Rows像DataTable.
How do I loop through the rows of a DataView?
如何遍历 DataView 的行?
回答by Alex Angas
The DataView object itself is used to loop through DataView rows.
DataView 对象本身用于循环遍历 DataView 行。
DataView rows are represented by the DataRowViewobject. The DataRowView.Rowproperty provides access to the original DataTable row.
DataView 行由DataRowView对象表示。该DataRowView.Row属性提供访问原始数据表中的行。
C#
C#
foreach (DataRowView rowView in dataView)
{
DataRow row = rowView.Row;
// Do something //
}
VB.NET
网络
For Each rowView As DataRowView in dataView
Dim row As DataRow = rowView.Row
' Do something '
Next
回答by Jon McCain
I prefer to do it in a more direct fashion. It does not have the Rows but is still has the array of rows.
我更喜欢以更直接的方式来做。它没有行,但仍然有行数组。
tblCrm.DefaultView.RowFilter = "customertype = 'new'";
qtytotal = 0;
for (int i = 0; i < tblCrm.DefaultView.Count; i++)
{
result = double.TryParse(tblCrm.DefaultView[i]["qty"].ToString(), out num);
if (result == false) num = 0;
qtytotal = qtytotal + num;
}
labQty.Text = qtytotal.ToString();
回答by Shujaat Kagathra
//You can convert DataView to Table. using DataView.ToTable();
//您可以将DataView转换为Table。使用 DataView.ToTable();
foreach (DataRow drGroup in dtGroups.Rows)
{
dtForms.DefaultView.RowFilter = "ParentFormID='" + drGroup["FormId"].ToString() + "'";
if (dtForms.DefaultView.Count > 0)
{
foreach (DataRow drForm in dtForms.DefaultView.ToTable().Rows)
{
drNew = dtNew.NewRow();
drNew["FormId"] = drForm["FormId"];
drNew["FormCaption"] = drForm["FormCaption"];
drNew["GroupName"] = drGroup["GroupName"];
dtNew.Rows.Add(drNew);
}
}
}
// Or You Can Use
// 2.
// 或者你可以使用
// 2.
dtForms.DefaultView.RowFilter = "ParentFormID='" + drGroup["FormId"].ToString() + "'";
DataTable DTFormFilter = dtForms.DefaultView.ToTable();
foreach (DataRow drFormFilter in DTFormFilter.Rows)
{
//Your logic goes here
}
回答by Behzad Ebrahimi
You can iterate DefaultViewas the following code by Indexer:
您可以DefaultView通过以下代码迭代Indexer:
DataTable dt = new DataTable();
// add some rows to your table
// ...
dt.DefaultView.Sort = "OneColumnName ASC"; // For example
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow oRow = dt.DefaultView[i].Row;
// Do your stuff with oRow
// ...
}

