0

只想选择星期一 10 和星期二 50。

 Monday    Tuesday    Wed
 10           40       9
 20           50       6
 30           70       4

我到目前为止的代码:

    For Each row As DataRow In table.Rows

        table2.Rows.Add(row(0), row(1))

    Next

这增加了周一和周二的所有数据,但我只想要周一的 1 个数据和周二的 1 个数据。?

起初我以为我需要有列,通过添加新行?

           For Each row As DataColumn In table.Columns
           table2.Rows.InsertAt(newRowb, 0)
           table2.Rows.Add(row*(0), row(1))

 ***getting mixed up with rows and columns, as am having errors on *

           Next

但是新行有错误,但是有什么方法可以添加特定数据

4

1 回答 1

1

您可以使用Linq-to-DataSet

Dim filteredRows = From row In table
                   Where  row.Field(Of Int32)("Monday") = 10 _
                   OrElse row.Field(Of Int32)("Tuesday") = 50
table2 = filteredRows.CopyToDataTable()

无论如何,是否使用循环来执行此操作,因为这是手动的。使用某种循环?

当然:

Dim table2 = table.Clone()
For Each row As DataRow In table.Rows
    Dim monCount = row.Field(Of Int32)("Monday")
    Dim tueCount = row.Field(Of Int32)("Tuesday")
    If monCount = 10 OrElse tueCount = 50 Then
        Dim newRow = table2.Rows.Add()
        newRow.ItemArray = row.ItemArray
    End If
Next
于 2013-07-22T10:24:00.680 回答