Asp.net 如何合并datatable中具有相同值的行(VB)

Asp.net 如何合并datatable中具有相同值的行(VB),asp.net,vb.net,datatable,datarow,merge-replication,Asp.net,Vb.net,Datatable,Datarow,Merge Replication,我有一个带有值的数据表tempDT: Serial_No testong --------------------------------------- DTSHCSN001205035919201 [ OUT ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012 DTSHCSN001205035919201 [ IN ] <br/> P

我有一个带有值的数据表tempDT:

Serial_No                      testong
---------------------------------------
DTSHCSN001205035919201         [ OUT ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201         [ IN ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201         [ OUT ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
DTSHCSN001205035919201         [ IN ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
调试时,结果是:

但我希望结果是这样的:

我已经更新了我的代码,因此它将查看列,而不是使用变量I

Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable

'initial the first and second columns
dtData.Columns.Add("Serial_No")
dtData.Columns.Add("1")

Dim i As Integer = 0
Dim row As DataRow

For Each row In tempDt.Rows
    Dim dr As DataRow
    i += 1
    'check if the serial no exists in the new Data Table
    If dtData.Select("Serial_No='" & row(0) & "'").Length > 0 Then



        'If found, then get the existing row
        dr = dtData.Select("Serial_No='" & row(0) & "'")(0)

        'Create new GridViewDataTextColumn
        Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
        colBaru.Caption = i
        colBaru.FieldName = i
        colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
        colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center


        colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
        colBaru.VisibleIndex = grid.VisibleColumns.Count
        colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
        colBaru.PropertiesTextEdit.EncodeHtml = False
        grid.Columns.Add(colBaru)

        'I assume you are adding the Number as the columns name
        'Only need to create if the Column doesn't exist
        If dtData.Columns.count - 1 < i Then
            dtData.Columns.Add(i.ToString) 
        End If

        'Use variable i here
        dr(i) = row(3)

        'Comment this out as you don't need to
        'dtData.Rows.Add(dr)
    Else

        'reset value of i
        i = 1

        'If not found, then create a new row  
        dr = dtData.NewRow
        ' i put this to add the serial_no value to datatable
        dr("Serial_No") = row(0)

        'for adding first value i with same row as serial_no
        Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
        colBaru.Caption = i
        colBaru.FieldName = i
        colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
        colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center


        colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
        colBaru.VisibleIndex = grid.VisibleColumns.Count
        colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
        colBaru.PropertiesTextEdit.EncodeHtml = False
        grid.Columns.Add(colBaru)

        'dtData.Columns.Add("1")

        'Would be better to use back variable i if you have reset it
        dr(i) = row(3)


        dtData.Rows.Add(dr)
    End If
Next
Dim tempDt=GetItemDataTable()
Dim dtData作为新数据表
'第一列和第二列的首字母
dtData.Columns.Add(“序列号”)
dtData.Columns.Add(“1”)
尺寸i为整数=0
将行变暗为数据行
对于tempDt.行中的每一行
将dr设置为数据行
i+=1
'检查新数据表中是否存在序列号
如果dtData.Select(“序列号=”&行(0)&“”)。长度>0,则
'如果找到,则获取现有行
dr=dtData。选择(“序列号=”&行(0)和“”)(0)
'创建新的GridViewDataTextColumn
Dim colBaru As GridViewDataTextColumn=新GridViewDataTextColumn()
colBaru.Caption=i
colBaru.FieldName=i
colBaru.CellStyle.HorizontalAlign=HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign=HorizontalAlign.Center
colBaru.UnboundType=DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex=grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString=“d1”
colBaru.PropertiesTextEdit.EncodeHtml=False
grid.Columns.Add(colBaru)
'我假设您正在添加数字作为列名称
'仅当列不存在时才需要创建
如果dtData.Columns.count-1
我已经更新了我的代码,因此它将查看列,而不是使用变量I

Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable

'initial the first and second columns
dtData.Columns.Add("Serial_No")
dtData.Columns.Add("1")

Dim i As Integer = 0
Dim row As DataRow

For Each row In tempDt.Rows
    Dim dr As DataRow
    i += 1
    'check if the serial no exists in the new Data Table
    If dtData.Select("Serial_No='" & row(0) & "'").Length > 0 Then



        'If found, then get the existing row
        dr = dtData.Select("Serial_No='" & row(0) & "'")(0)

        'Create new GridViewDataTextColumn
        Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
        colBaru.Caption = i
        colBaru.FieldName = i
        colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
        colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center


        colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
        colBaru.VisibleIndex = grid.VisibleColumns.Count
        colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
        colBaru.PropertiesTextEdit.EncodeHtml = False
        grid.Columns.Add(colBaru)

        'I assume you are adding the Number as the columns name
        'Only need to create if the Column doesn't exist
        If dtData.Columns.count - 1 < i Then
            dtData.Columns.Add(i.ToString) 
        End If

        'Use variable i here
        dr(i) = row(3)

        'Comment this out as you don't need to
        'dtData.Rows.Add(dr)
    Else

        'reset value of i
        i = 1

        'If not found, then create a new row  
        dr = dtData.NewRow
        ' i put this to add the serial_no value to datatable
        dr("Serial_No") = row(0)

        'for adding first value i with same row as serial_no
        Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
        colBaru.Caption = i
        colBaru.FieldName = i
        colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
        colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center


        colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
        colBaru.VisibleIndex = grid.VisibleColumns.Count
        colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
        colBaru.PropertiesTextEdit.EncodeHtml = False
        grid.Columns.Add(colBaru)

        'dtData.Columns.Add("1")

        'Would be better to use back variable i if you have reset it
        dr(i) = row(3)


        dtData.Rows.Add(dr)
    End If
Next
Dim tempDt=GetItemDataTable()
Dim dtData作为新数据表
'第一列和第二列的首字母
dtData.Columns.Add(“序列号”)
dtData.Columns.Add(“1”)
尺寸i为整数=0
将行变暗为数据行
对于tempDt.行中的每一行
将dr设置为数据行
i+=1
'检查新数据表中是否存在序列号
如果dtData.Select(“序列号=”&行(0)&“”)。长度>0,则
'如果找到,则获取现有行
dr=dtData。选择(“序列号=”&行(0)和“”)(0)
'创建新的GridViewDataTextColumn
Dim colBaru As GridViewDataTextColumn=新GridViewDataTextColumn()
colBaru.Caption=i
colBaru.FieldName=i
colBaru.CellStyle.HorizontalAlign=HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign=HorizontalAlign.Center
colBaru.UnboundType=DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex=grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString=“d1”
colBaru.PropertiesTextEdit.EncodeHtml=False
grid.Columns.Add(colBaru)
'我假设您正在添加数字作为列名称
'仅当列不存在时才需要创建
如果dtData.Columns.count-1
感谢nick的回复,但它仍然没有给出正确的解决方案,但很快就会出现。您忘记了什么,我在dr=dtData.NewRow之后添加了dr(Serial_No)=row(0)if else条件来添加Serial_No。现在的问题是第一个循环中的row(3)的值没有添加到datatable。所以在第(0)行添加之后,他们读取第(3)行的第二个值。我已尝试将dr(i)=第(3)行置于else状态。但是它不起作用。只是想知道如果你把
dr(Serial_No)=行(0)
放在if-else条件之前,你的条件是否应该始终为真?所以它不会创建新行,是吗?另外,因为我是在if侧声明的,所以您只需要将
dtData.Columns.Add(1)
dr(0)=行(3)yap这是正确的,但如果您没有