1

value を持つデータテーブル tempDT が 1 つあります。

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 を 1 つの行にマージして、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

デバッグすると、結果は次のようになります。 間違った結果

しかし、私は結果が次のようになりたかった: ここに画像の説明を入力

4

1 に答える 1

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
于 2012-05-14T01:19:59.387 に答える