2009-09-15 12 views

Répondre

114

L'objet DataView lui-même est utilisé pour effectuer une boucle sur les lignes DataView.

Les lignes DataView sont représentées par l'objet DataRowView. La propriété DataRowView.Row permet d'accéder à la ligne DataTable d'origine.

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 
2

// Vous pouvez convertir DataView au tableau. en utilisant 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); 
     } 
    } 
} 

// Vous pouvez aussi utiliser

// 2.

dtForms.DefaultView.RowFilter = "ParentFormID='" + drGroup["FormId"].ToString() + "'"; 

DataTable DTFormFilter = dtForms.DefaultView.ToTable(); 

foreach (DataRow drFormFilter in DTFormFilter.Rows) 
{ 
          //Your logic goes here 
} 
0

Je préfère le faire d'une façon plus directe. Il n'a pas les lignes mais a toujours le tableau de lignes.

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();