DataView
对象没有像Rows
这样的DataTable
属性。
如何遍历DataView的行?
答案 0 :(得分:125)
DataView对象本身用于循环遍历DataView行。
DataView行由DataRowView对象表示。 DataRowView.Row属性提供对原始DataTable行的访问。
<强> 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
答案 1 :(得分:2)
//您可以将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);
}
}
}
//或者你可以使用
// 2。
dtForms.DefaultView.RowFilter = "ParentFormID='" + drGroup["FormId"].ToString() + "'";
DataTable DTFormFilter = dtForms.DefaultView.ToTable();
foreach (DataRow drFormFilter in DTFormFilter.Rows)
{
//Your logic goes here
}
答案 2 :(得分:1)
我更喜欢以更直接的方式来做。它没有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();
答案 3 :(得分:0)
您可以通过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
// ...
}