所以我有一个如下所示的数据集
Col1 Col2 Col3 Col4
Apple 1 10 Orange
Apple 2 20 Orange
Apple 3 30 Orange
Apple 1 10 Pear
Apple 2 20 Pear
Apple 3 30 Pear
Orange 1 10 grapes
Orange 2 20 grapes
ORange 1 10 kiwi
Berries 1 10 apple
Berries 1 20 Kiwi
我只需要像
这样的东西 Col1 Col2 Col3 Col4
Apple 1 10 Orange
Apple 2 20 Orange
Apple 3 30 Orange
ORange 1 10 Grapes
Orange 2 20 Grapes
Berries 1 10 Apple
所以基本上它是col1和col4,如果col4在我读完行时改变col1,我应该显示
有人可以帮助我吗
根据第一个建议
For i As Integer = 0 To dtResults.Rows.Count - 1
Dim firstItem As String = dtResults.Rows(i)("col1").ToString()
Dim firstToB As String = dtResults.Rows(i)("col4").ToString()
dtResults.DefaultView.RowFilter = "col4= '" + firstToB + "'"
Dim tempTable As DataTable = dtResults.DefaultView.ToTable()
Dim Total As Integer = 0
For Each dr As DataRow In tempTable.Rows
'Dim firstItem As String = dr("col1").ToString()
'If (dr("col1") = firstItem) AndAlso (firstToBin = dr("col4")) Then
If item Is Nothing OrElse item <> dr("col1") Then
If Not item Is Nothing Then
dgv.Rows.Add()
End If
itemnum = dr("col1")
Else
itemnum = ""
desc = ""
size = ""
Total += dr("col3")
End If
item = dr("col1")
dgv.Rows.Add(dr('',dr('',dr('')...)
count += 1
'End If
Next
Next
答案 0 :(得分:0)
您需要过滤DataTable的行。您可以使用DefaultView来执行此操作。
例如:
Dim ds1 As New DataSet1 'Create an instance of your DataSet1
ds1.yourDataTable.DefaultView.RowFilter = "Col4='Orange'"
如果您的DataSet的DataTable是在Visual Studio的设计器中创建的,则可以为每列指定DataType(字符串,整数,布尔值)。默认情况下,DataType设置为字符串。
你可以用两种方式做到这一点
答案 1 :(得分:0)
所以要实现这里提到的问题就是我所做的。我总是可以在存储过程中进行更改,但我应该能够在数据集中查看这些行以用于其他目的。因此,为了绑定网格,我采用了行的项目,如下面循环
For i As Integer = 0 To dtResults.Rows.Count - 1
Dim firstItem As String = dtResults.Rows(i)("col1").ToString()
If acceptitem Is Nothing OrElse acceptitem <> firstItem Then
Dim firstToB As String = dtResults.Rows(i)("col4").ToString()
dtResults.DefaultView.RowFilter = "col4 = '" + firstToB + "' AND col1 = '" + firstItem + "'"
Dim tempTable As DataTable = dtResults.DefaultView.ToTable()
Dim Total As Integer = 0 ' dtResults.Rows(i)("col3").ToString()
For Each dr As DataRow In tempTable.Rows
acceptitem = dr("col1").ToString()
If item Is Nothing OrElse item <> dr("col1") Then
If Not item Is Nothing Then
dgv.Rows.Add()
End If
itemnum = dr("col1")
Total += Convert.ToInt16(dr("col3").ToString())
Else
itemnum = ""
Total += Convert.ToInt16(dr("col3").ToString())
End If
item = dr("col1")
dgv.Rows.Add(dr('',dr('',dr('').......)
count += 1
Next
End If
Next
非常感谢所有其他建议......