根据列表视图项启用按钮

时间:2013-11-24 11:27:02

标签: vb.net visual-studio-2010 visual-studio

我需要一些帮助,因为我是vb.net的新用户,启用了基于事件的按钮。场景是我有一个listview,它根据访问查询一次填充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

1 个答案:

答案 0 :(得分:1)

按钮是否因为您遇到异常而未启用,那么是否跳出代码?尝试捕获之外你可以放下像下面这样的东西

btnNextPostings.Enabled = ListView1.Items.Count > 0