2014-02-11 44 views
0

如果2行有相同的日期,但我現在需要把彼此跟隨的日子放在一起,並有Journee。我怎樣才能做到這一點,嘗試這種代碼,但它不能正常工作,在此先感謝如果數據顯示在asp:標籤使用vb.net使用日期字符串

私人小組BindRepeaterData(BYVAL noformationL作爲字符串)

Dim conSQL As New System.Data.SqlClient.SqlConnection 
    Dim cmdSQL As New System.Data.SqlClient.SqlCommand 

    conSQL.ConnectionString = Application("BACKstr") 
    conSQL.Open() 

    cmdSQL.Connection = conSQL 
    cmdSQL.CommandText = "select convert(char,[DateFormation],103) AS DateFormation,[DemiJournee] from [EXT$FORM_FormationDates] where NoFormation='" & Replace(noformationL, "'", "''") & "' and TypeDates = 0 order by DateFormation,DemiJourneeInt" 

    Dim ds As New System.Data.DataSet() 
    Dim da As New Data.SqlClient.SqlDataAdapter(cmdSQL) 

    da.Fill(ds) 

    If ds.Tables(0).Rows.Count > 0 Then 
     'On fait un premier nouveau dataset avec table pour y mettre les données finales de DemiJournee    
     Dim dsfinal As New System.Data.DataSet() 
     dsfinal.Tables.Add("FinalData") 
     dsfinal.Tables("FinalData").Columns.Add("DateFormation") 
     dsfinal.Tables("FinalData").Columns.Add("DemiJournee") 
     'On suit le DS en comparant les rows et additionant a dsfinal et puis binding dsfinal to repeater 
     Dim totalrows = ds.Tables(0).Rows.Count 
     Dim rowpointer As Integer = 0 
     Dim r = 0 
     If totalrows > 2 Then 
      'Si nous avons plus de 2 rows 
      'On compare 1 row avec la suivante row jusqu'arrivé a les deux derniere rows     
      Do While r < totalrows - 2 
       If ds.Tables(0).Rows(r).Item("DateFormation") = ds.Tables(0).Rows(r + 1).Item("DateFormation") Then 
        'Les deux rows a comparé on la meme date 
        'Nous mettons seulemenent un row au lieu de deux dans la table 
        dsfinal.Tables("FinalData").Rows.Add(ds.Tables(0).Rows(r).Item("DateFormation"), "Journée")       
        r += 2 '+1 to r 
        rowpointer = r '+1 to rowpointer 
       Else 
        'Les 2 rows on differentes dates 
        'On met en premier le comparateur comme row. 
        'On laisse la suivante row pour la comparer avec la row apres elle. 
        dsfinal.Tables("FinalData").Rows.Add(ds.Tables(0).Rows(r).Item("DateFormation"), ds.Tables(0).Rows(r).Item("DemiJournee")) 
        r += 1 
        rowpointer = r 
       End If 
      Loop 

     End If 

     If rowpointer < totalrows - 1 Then 
      'On a besoin de comparer les deux restantes rows 
      If ds.Tables(0).Rows(rowpointer).Item("DateFormation") = ds.Tables(0).Rows(rowpointer + 1).Item("DateFormation") Then 
       'Les deux rows comparées on la meme date 
       'Nous mettons seulement 1 row dans la table 
       dsfinal.Tables("FinalData").Rows.Add(ds.Tables(0).Rows(rowpointer).Item("DateFormation"), "Journée") 
      Else 
       'Les 2 rows on dates différentes 
       'On mets les 2 rows dans la table 
       dsfinal.Tables("FinalData").Rows.Add(ds.Tables(0).Rows(rowpointer).Item("DateFormation"), ds.Tables(0).Rows(rowpointer).Item("DemiJournee")) 
       dsfinal.Tables("FinalData").Rows.Add(ds.Tables(0).Rows(rowpointer + 1).Item("DateFormation"), ds.Tables(0).Rows(rowpointer + 1).Item("DemiJournee")) 
      End If 
     Else 
      'Nous avons seulement un historique(record) 
      dsfinal.Tables("FinalData").Rows.Add(ds.Tables(0).Rows(rowpointer).Item("DateFormation"), ds.Tables(0).Rows(rowpointer).Item("DemiJournee")) 
     End If 

     ds = Nothing 

     If dsfinal.Tables(0).Rows.Count > 0 Then 
      'On fait un premier nouveau dataset avec table pour y mettre les données finales de DemiJournee    
      Dim dsfinal2 As New System.Data.DataSet() 
      dsfinal2.Tables.Add("FinalData") 
      dsfinal2.Tables("FinalData").Columns.Add("DateFormation") 
      dsfinal2.Tables("FinalData").Columns.Add("DemiJournee") 
      'On suit le DS en comparant les rows et additionant a dsfinal et puis binding dsfinal to repeater 
      Dim totalrows2 = dsfinal.Tables(0).Rows.Count 
      Dim rowpointer2 As Integer = 0 
      Dim r2 = 0 
      If totalrows2 > 2 Then 
       'Si nous avons plus de 2 rows 
       'On compare 1 row avec la suivante row jusqu'arrivé a les deux derniere rows     
       Do While r2 < totalrows2 - 2 
        If dsfinal.Tables(0).Rows(r2).Item("DemiJournee") = "Journée" Then 
         'Les deux rows a comparé on la meme date 
         'Nous mettons seulemenent un row au lieu de deux dans la table 
         dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(r2).Item("DateFormation") + " au " + dsfinal.Tables(0).Rows(r2 + 1).Item("DateFormation"), "Journée") 
         r2 += 1 '+1 to r 
         rowpointer2 = r2 '+1 to rowpointer 
        Else 
         'Les 2 rows on differentes dates 
         'On met en premier le comparateur comme row. 
         'On laisse la suivante row pour la comparer avec la row apres elle. 
         dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(r2).Item("DateFormation"), dsfinal.Tables(0).Rows(r2).Item("DemiJournee")) 
         dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(r2 + 1).Item("DateFormation"), dsfinal.Tables(0).Rows(r2 + 1).Item("DemiJournee")) 
         r2 += 2 
         rowpointer2 = r2 
        End If 
       Loop 

      End If 

      If rowpointer2 < totalrows2 - 1 Then 
       'On a besoin de comparer les deux restantes rows 
       If dsfinal.Tables(0).Rows(r2).Item("DemiJournee") = "Journée" Then 
        'Les deux rows comparées on la meme date 
        'Nous mettons seulement 1 row dans la table 
        dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(rowpointer2).Item("DateFormation"), "Journée") 
       Else 
        'Les 2 rows on dates différentes 
        'On mets les 2 rows dans la table 
        dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(rowpointer2).Item("DateFormation"), dsfinal.Tables(0).Rows(rowpointer2).Item("DemiJournee")) 
        dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(rowpointer2 + 1).Item("DateFormation"), dsfinal.Tables(0).Rows(rowpointer2 + 1).Item("DemiJournee")) 
       End If 
      Else 
       'Nous avons seulement un historique(record) 
       dsfinal2.Tables("FinalData").Rows.Add(dsfinal.Tables(0).Rows(rowpointer2).Item("DateFormation"), dsfinal.Tables(0).Rows(rowpointer2).Item("DemiJournee")) 
      End If 

      'clear le ds original 
      dsfinal = Nothing 

      rep_Dates.Visible = True 
      rep_Dates.DataSource = dsfinal2 
      rep_Dates.DataBind() 

      dsfinal2 = Nothing 

     Else 
      rep_Dates.Visible = False 
     End If 

    Else 
     rep_Dates.Visible = False 
    End If 

    conSQL.Close() 

End Sub 
+0

我剛發現的溶液,我想,我改變了代碼到 –

+0

做,當R2

+0

看起來不錯...檢查代碼是否正常工作(如果只返回一條記錄)檢查最後一條記錄是否也被考慮不遺漏。這些是我在初次排序時遇到的問題。否則做得好! – Mych

回答

0

OK拉斐爾......我的解決方案... 我創建了兩個中繼器,以便我可以看到第一個轉換和第二個轉換。您可以將最後一次轉換綁定到您擁有的中繼器上...

任何問題...請詢問。

Private Sub BindRepeaterData() 
    'For test I removed param 

    Dim conSQL As New System.Data.SqlClient.SqlConnection 
    Dim cmdSQL As New System.Data.SqlClient.SqlCommand 

    conSQL.ConnectionString = ConfigurationManager.ConnectionStrings("TestConn").ToString 
    'Change your connection string... 
    conSQL.Open() 

    cmdSQL.Connection = conSQL 
    cmdSQL.CommandText = "select convert(char,[DateFormation],103) AS DateFormation,[DemiJournee] from [TblTest] order by DateFormation" 
    'Change your Sql 

    Dim ds As New System.Data.DataSet() 
    Dim da As New Data.SqlClient.SqlDataAdapter(cmdSQL) 

    da.Fill(ds) 

    If ds.Tables(0).Rows.Count > 0 Then 

     'Create a new dataset with table where we will store new data 
     Dim dsTemp As New DataSet() 
     dsTemp.Tables.Add("TempData") 
     dsTemp.Tables("TempData").Columns.Add("DateFormation") 
     dsTemp.Tables("TempData").Columns.Add("DemiJournee") 

     'Now we will go through ds comparing two rows at a time. 
     'If the two rows have the same date the only add one row to TempData changing the DemiJournee field to Journée. 
     'Else if not the same date then add one row but leave the second as we need to next compare it with its successor. 

     Dim totalrows = ds.Tables(0).Rows.Count 
     Dim rowpointer As Integer = 0 
     Dim r = 0 
     If totalrows > 2 Then 
      'We have more than two rows 
      'We compare one row against the next row until we get to the last two rows. 
      Do While r < totalrows - 2 
       If ds.Tables(0).Rows(r).Item("DateFormation") = ds.Tables(0).Rows(r + 1).Item("DateFormation") Then 
        'Both compared rows have same date 
        'We add just one row to table and change DemiJournee field to Journée 
        dsTemp.Tables("TempData").Rows.Add(ds.Tables(0).Rows(r).Item("DateFormation"), "Journée") 
        r += 2 'add 2 to r (we do not need to compare the second row as we have just condenced it) 
        rowpointer = r 'add 2 to rowpointer 
       Else 
        'The two rows have different dates 
        'add first comparitor as a row. 
        'we leave the second row to make comparision with the row after it 
        dsTemp.Tables("TempData").Rows.Add(ds.Tables(0).Rows(r).Item("DateFormation"), ds.Tables(0).Rows(r).Item("DemiJournee")) 
        r += 1 
        rowpointer = r 
       End If 
      Loop 

     End If 

     If rowpointer < totalrows - 1 Then 
      'We need to compare the remaining two rows 
      If ds.Tables(0).Rows(rowpointer).Item("DateFormation") = ds.Tables(0).Rows(rowpointer + 1).Item("DateFormation") Then 
       'Both compared rows have same date 
       'We add just one row to table 
       dsTemp.Tables("TempData").Rows.Add(ds.Tables(0).Rows(rowpointer).Item("DateFormation"), "Journée") 
      Else 
       'The two rows have different dates 
       'add both rows 
       dsTemp.Tables("TempData").Rows.Add(ds.Tables(0).Rows(rowpointer).Item("DateFormation"), ds.Tables(0).Rows(rowpointer).Item("DemiJournee")) 
       dsTemp.Tables("TempData").Rows.Add(ds.Tables(0).Rows(rowpointer + 1).Item("DateFormation"), ds.Tables(0).Rows(rowpointer + 1).Item("DemiJournee")) 
      End If 
     Else 
      'We only have one record 
      dsTemp.Tables("TempData").Rows.Add(ds.Tables(0).Rows(rowpointer).Item("DateFormation"), ds.Tables(0).Rows(rowpointer).Item("DemiJournee")) 
     End If 

     'dsTemp now contains the corrected data. 

     rep_Dates.Visible = True 
     rep_Dates.DataSource = dsTemp 
     rep_Dates.DataBind() 

     'clear original ds so that we can sort our data again 
     ds.Tables(0).Clear() 


     'The second sort needs to go through TempData comparing two rows but this time compare the DemiJournee field 
     'If the two rows have Journée as their DemiJournee value AND the dates are sequential then store then as 
     '10/01/2014 - 11/01/2014 Journée... continue comparing with the next row. If that is Journée and a sequential date 
     'then we would need to store as 10/01/2014 - 12/01/2014 Journée and so on. 
     'If not sequential or not Journée then store as original. 

     Dim sd As String = "" 'used to store the start date of our sequence 
     Dim cd As String = "" 'used to store the compare date of our sequence 
     Dim ed As String = "" 'used to store the end date or our sequence 

     totalrows = dsTemp.Tables("TempData").Rows.Count 
     rowpointer = 0 
     r = 0 

     If totalrows > 2 Then 
      'We have more than two rows 
      'We compare one row against the next row until we get to the last two rows. 
      Do While r < totalrows - 2 
       If dsTemp.Tables("TempData").Rows(r).Item("DemiJournee") = "Journée" And dsTemp.Tables("TempData").Rows(r + 1).Item("DemiJournee") = "Journée" Then 
        'Both compared rows have DemiJournee = "Journée" 
        If sd = "" Then 
         'We have no startdate so start a new sequence 
         sd = dsTemp.Tables("TempData").Rows(r).Item("DateFormation") 
         cd = dsTemp.Tables("TempData").Rows(r).Item("DateFormation") 
         ed = dsTemp.Tables("TempData").Rows(r + 1).Item("DateFormation") 

         If CDate(ed) = DateAdd(DateInterval.Day, 1, CDate(cd)) Then 
          'The two date are sequential... 
          'We don't add any rows until we run out of compared rows or sequential dates 
          r += 1 
          rowpointer = r 
         Else 
          'The two dates are not sequential. We add the first row and leave the second to compare with the next row in line 
          ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(r).Item("DateFormation"), dsTemp.Tables("TempData").Rows(r).Item("DemiJournee")) 
          r += 1 
          rowpointer = r 
          sd = "" 
          cd = "" 
          ed = "" 
         End If 
        Else 
         'we already have a start date 
         cd = dsTemp.Tables("TempData").Rows(r).Item("DemiJournee") 
         ed = dsTemp.Tables("TempData").Rows(r + 1).Item("DemiJournee") 
         If CDate(ed) = DateAdd(DateInterval.Day, 1, CDate(cd)) Then 
          'The two date are sequential... 
          'We don't add any rows until we run out of compared rows or sequential dates 
          r += 1 
          rowpointer = r 
         Else 
          'The two dates are not sequential. 
          'Our sequence has broken so we finish this sequence by adding a new row 
          'sd - ed Journée 
          ds.Tables(0).Rows.Add(sd.ToString & " - " & ed.ToString, "Journée") 
          r += 1 
          rowpointer = r 
          sd = "" 
          cd = "" 
          ed = "" 
         End If 
        End If 
       Else 
        'The two rows have different DemiJournee 
        If sd = "" Then 
         'We have no squence so 
         'add first comparitor as a row. 
         'we leave the second row to make comparision with the row after it 
         ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(r).Item("DateFormation"), dsTemp.Tables("TempData").Rows(r).Item("DemiJournee")) 
         r += 1 
         rowpointer = r 
        Else 
         'we have a sequence and our sequence has broken so we finish this sequence by adding a new row 
         ds.Tables(0).Rows.Add(sd.ToString & " - " & ed.ToString, "Journée") 
         r += 1 
         rowpointer = r 
         sd = "" 
         cd = "" 
         ed = "" 
        End If 

       End If 
      Loop 
     End If 

     'Now finish off with remaining rows 
     If rowpointer < totalrows - 1 Then 
      'We need to compare the remaining two rows 
      If dsTemp.Tables("TempData").Rows(rowpointer).Item("DemiJournee") = "Journée" And dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DemiJournee") = "Journée" Then 
       'Both compared rows have DemiJournee = "Journée" 
       If sd = "" Then 
        'We have no startdate so start a new sequence 
        sd = dsTemp.Tables("TempData").Rows(rowpointer).Item("DateFormation") 
        cd = dsTemp.Tables("TempData").Rows(rowpointer).Item("DateFormation") 
        ed = dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DateFormation") 

        If CDate(ed) = DateAdd(DateInterval.Day, 1, CDate(cd)) Then 
         'The two date are sequential... 
         'These are the last two rows so we ad one condenced row 
         ds.Tables(0).Rows.Add(sd.ToString & " - " & ed.ToString, "Journée") 
        Else 
         'The two dates are not sequential. We add two new rows 
         ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer).Item("DateFormation"), dsTemp.Tables("TempData").Rows(rowpointer).Item("DemiJournee")) 
         ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DateFormation"), dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DemiJournee")) 
        End If 
       Else 
        'we already have a start date 
        cd = dsTemp.Tables("TempData").Rows(r).Item("DemiJournee") 
        ed = dsTemp.Tables("TempData").Rows(r + 1).Item("DemiJournee") 
        If CDate(ed) = DateAdd(DateInterval.Day, 1, CDate(cd)) Then 
         'The two date are sequential... 
         'These are the last two rows so we ad one condenced row 
         ds.Tables(0).Rows.Add(sd.ToString & " - " & ed.ToString, "Journée") 
        Else 
         'The two dates are not sequential. 
         'Our sequence has broken so we finish this sequence by adding a new row 
         'sd - ed Journée 
         ds.Tables(0).Rows.Add(sd.ToString & " - " & cd.ToString, "Journée") 
         'plus the last row 
         ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DateFormation"), dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DemiJournee")) 
        End If 
       End If 
       ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer).Item("DateFormation"), "Journée") 
      Else 
       'The two rows have different dates 
       'add both rows 
       ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer).Item("DateFormation"), dsTemp.Tables("TempData").Rows(rowpointer).Item("DemiJournee")) 
       ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DateFormation"), dsTemp.Tables("TempData").Rows(rowpointer + 1).Item("DemiJournee")) 
      End If 
     Else 
      'We only have one record 
      ds.Tables(0).Rows.Add(dsTemp.Tables("TempData").Rows(rowpointer).Item("DateFormation"), dsTemp.Tables("TempData").Rows(rowpointer).Item("DemiJournee")) 
     End If 



     rep_DemiJournee.Visible = True 
     rep_DemiJournee.DataSource = ds 
     rep_DemiJournee.DataBind() 

     dsTemp = Nothing 
     ds = Nothing 
    Else 
     rep_Dates.Visible = False 
     rep_DemiJournee.Visible = False 
    End If 

    conSQL.Close() 

End Sub 

結果您所提供給數據...

rep_Dates

日期
15/01/2014 JOURNEE
16/01/2014 JOURNEE
17/01/2014 Matin
20/01/2014Après-midi
個27/01/2014滑完-MIDI
28/01/2014晨報
29/01/2014晨報

rep_DemiJouree

日期
15/01/2014 - 16/01/2014 JOURNEE
17/01/2014晨報
20/01/2014滑完-MIDI
27/01/2014滑完-MIDI
28/01/2014晨報
29/01/2014馬丁

+0

MyHC的是太棒了,再次感謝,它完美地解決了我的問題,我的理解everybit它。當更多的問題也就來了,我會讓你知道,如果你可以幫助我學習。再次恭喜。 –

相關問題