【问题标题】:how to merge rows in datatable which had same value (VB)如何合并数据表中具有相同值的行(VB)
【发布时间】:2012-05-21 04:24:44
【问题描述】:

我有一个带有值的数据表 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

问题是我想将重复的serial_no合并到一行中,testong的值添加到新列中。

我尝试了很多方法,但我找不到解决方案。

这是我的代码:

    Dim tempDt = GetItemDataTable()
    Dim dtData As New DataTable

    dtData.Columns.Add("Serial_No")
    Dim i As Integer = 0
    Dim row As DataRow

    For Each row In tempDt.Rows
        i += 1
        Dim dr As DataRow = dtData.NewRow
        dr("Serial_No") = row(0)
        If dr("Serial_No") = row(0) Then
            Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
            colBaru.Caption = i
            colBaru.FieldName = i
            colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
            colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            colBaru.Width = 150

            colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
            colBaru.VisibleIndex = grid.VisibleColumns.Count
            colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
            colBaru.PropertiesTextEdit.EncodeHtml = True
            grid.Columns.Add(colBaru)
            dtData.Columns.Add(i)
            dr(i) = row(3)
            dtData.Rows.Add(dr)

        Else
            dr("Serial_No") = row(0)
            dtData.Rows.Add(dr)
        End If

当我调试结果是:

但我想要的结果是这样的:

【问题讨论】:

    标签: asp.net vb.net datatable datarow merge-replication


    【解决方案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
    

    【讨论】:

    • 感谢 nick 的回复,但它仍然没有给出正确的解决方案,但它就在附近。您忘记了一些事情,我将 dr(Serial_No) = row(0) if else 条件放在 dr= dtData.NewRow 之后添加 Serial_No 。现在的问题是第一个循环中 row(3) 的值未添加到数据表中。所以在添加行(0)之后,他们读取了行(3)的第二个值。我曾尝试将 dr(i) = row(3) 置于其他条件下。但它不起作用。
    • 只是想知道如果您将dr(Serial_No) = row(0) 放在 if else 条件之前,您的条件是否应该始终为真?所以它不会创建一个新行,对吗?也因为 i 是在 if 中声明的,所以你只需要把 dtData.Columns.Add(1) 然后 dr(0) = row(3)
    • yap 是对的,但是如果您不将 dr(serial) = row(0) 置于 else 条件下,它将不会插入任何值。我修改了你的答案。及其正常工作。无论如何,非常感谢......今天是我这个模块的最后期限。呵呵,你是我的救星。 :P
    • 抱歉打扰你了。但我仍然有不正确的结果。当我有新行时,参数 i+=1 总是循环包含。 i.stack.imgur.com/AC7fp.jpg
    • 它仍然给出一个。对于每一列,我都会收到错误 indexoutofrange "System.IndexOutOfRangeException: Cannot find column 1."
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    相关资源
    最近更新 更多