2012-05-13 49 views
1

我有值的一個數據表tempDT:如何合併在數據表中的行其中有相同的值(VB)

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 

當我調試的結果是: wrong result

但我想要的結果是這樣的: enter image description here

回答

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 
+0

感謝尼克爲您的迴應,但它仍然沒有給出正確的解決方案,但它的附近。你忘記了一些東西,我把dr(Serial_No)= row(0)if else after dr = dtData.NewRow加入Serial_No。現在問題是第一個循環中的行(3)的值沒有添加到數據表中。所以行(0)添加後,他們讀取行(3)的第二個值。我曾嘗試把博士(我)=行(3)在其他條件。但它不起作用。 – tyo

+0

只是想知道,如果在if else條件之前放入'dr(Serial_No)= row(0)',你的條件總是正確嗎?所以它不會創建一個新的行,是嗎?另外,因爲我是在一邊聲明,所以你只需要把'dtData.Columns.Add(1)'然後'dr(0)= row(3)' – Nick

+0

yap這是正確的,但如果你不把博士(serial)= row(0)in else condition,它不會插入任何值。我修改了你的答案。並且其工作正常。無論如何非常感謝...今天是這個模塊的最後期限。呵呵,你是我的救星。 :P – tyo

相關問題