该DataView
对象没有Rows
像DataTable
.
如何循环遍历 DataView 的行?
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
//可以将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
}
我更喜欢用更直接的方式来做。它没有行,但仍然有行数组。
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();
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
// ...
}