2013-11-24 26 views
0

我需要一些幫助,因爲我是啓用基於事件的按鈕的vb.net的新用戶。該場景是我有一個列表視圖基於訪問查詢一次填充100個記錄。似乎有兩個主要的編碼領域。基於列表視圖項啓用按鈕

1)啓用「下一步」按鈕,但前提是數據庫中有超過100條記錄。這意味着我需要首先運行查詢來獲取總行數。將此計數存儲在類級變量中,然後在填充列表時,如果此計數大於100,則啓用下一個按鈕。我也需要保持當前的頁碼數量。當用戶按下Next按鈕時,它會加載下100行,然後檢查是否仍需要通過檢查PageNumber * 100是否小於行數來啓用Next按鈕。

2)必須使上一個按鈕,如果當前頁面數不爲1

我將不勝感激,如果有人能夠幫助這或點我獲得進一步幫助的方向。非常感謝

Sub filllistview() 
     Try 
      'creatconn() 
      oledbCnn.ConnectionString = My.Settings.storageConnectionString 
      oledbCnn.Open() 
      Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings WHERE Customer = '" & newvar & "' ORDER BY Date DESC", oledbCnn) 
      dr = oledbCmd.ExecuteReader() 

      Dim tmpColor As Color = Color.Green 

      'clear items in the list before populating with new values 
      ListView1.Items.Clear() 

      While dr.Read() 

       ListView1.Items.Add(CDate(dr(4)).ToShortDateString()).UseItemStyleForSubItems = False 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1).ToString()) 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString()) 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString()) 
       With ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C")) 
        If CDbl(dr(5)) < 0 Then 
         .ForeColor = Color.Red 
         .BackColor = Color.Gainsboro 
         '.Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold) 
        Else 
         .ForeColor = tmpColor 
        End If 
       End With 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString()) 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString()) 


      End While 

      'autosize each column in listview 
      For Each col As ColumnHeader In ListView1.Columns 
       col.Width = -2 
      Next col 

      'refresh the list with new data 
      ListView1.Refresh() 

      'Enable the posting previous button 
      'btnNextPostings.Enabled = True 

     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     Finally 
      dr.Close() 
      oledbCnn.Close() 
     End Try 
    End Sub 
+0

您可以考慮使用[bindingNavigator(http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingnavigator (v = vs.110)的.aspx)。它由下一個,之前的aso按鈕組成或在[wpf]中使用somthing euqivalent(http://social.msdn.microsoft.com/Forums/en-US/1f392098-0e52-47fc-9338-ce1f7edbe61c/binding-navigator- in-wpf-xaml) –

+0

是你要求的winForms或WPF嗎? –

+0

winforms。謝謝 – user1532468

回答

1

該按鈕沒有被啓用,因爲你正在打一個異常,所以然後跳出代碼?在嘗試捕捉之外,你可以把類似的follwing

btnNextPostings.Enabled = ListView1.Items.Count > 0 
+0

這工作正常,但它不允許編碼按鈕來顯示列表中的下100個記錄。 – user1532468