2012-11-28 63 views
2

我想通過索引從數據行中的特定列中獲取值,我在訪問返回的第二行中的值時遇到問題,下面是我試圖實現的目標VB.Net訪問數據表按行索引並獲取字段值

  Dim daSeries As New dsSVTableAdapters.clsCH 
      Dim dtSeries As New dsSV.SeriesDataTable 
      Dim drSeries As dsSV.SeriesRow 

      dtSeries = daSeries.CSeries(1) 
      drSeries = dtSeries.Rows(0) 

      Dim RowCnt As Integer = dtSeries.Rows.Count 'Current RowCnt is 2 

       Select Case RowCnt 

        Case 1 'Only One row exists 
         hxValue1.Value = drSeries.YFieldName 'access 1st row YFieldName 
         hyValue.Value = drSeries.XFieldName 'access 1st row XFieldName 
        Case 2 'Two rows exists 
         For i As Integer = 0 To dtSeries.Rows.Count - 1 
          If i = 0 Then 'First Row index 
           hxValue1.Value = drSeries.YFieldName 'access 1st row YFieldName 
           hyValue.Value = drSeries.XFieldName 'access 1st row XFieldName 
          ElseIf i = 1 Then '2nd Row index 
           hxValue2.Value = drSeries.YFieldName 'access 2nd row YFieldName 
          End If 
         Next 

       End Select 

回答

0

您的drSeries變量點總是在第一行。您應該更改爲循環內的第二行

Case 2 'Two rows exists 
    For i As Integer = 0 To dtSeries.Rows.Count - 1 
     ' Simply add this to your loop 
     drSeries = dtSeries.Rows(i) 

     If i = 0 Then 'First Row index 
      hxValue1.Value = drSeries.YFieldName 'access 1st row YFieldName 
      hyValue.Value = drSeries.XFieldName 'access 1st row XFieldName 
     ElseIf i = 1 Then '2nd Row index 
      hxValue2.Value = drSeries.YFieldName 'access 2nd row YFieldName 
     End If 
    Next 
+0

啊關鍵成分謝謝@Steve – user1588253