user1532468
user1532468

Reputation: 1751

Enable button based on listview items

I need some help as I am a new user to vb.net with enabling a button based on an event. The scenario is I have a listview which populates 100 records at a time based on a access query. There seem to be 2 main areas of coding.

1) Enable Next button but only if there are more than 100 records in database. This means that I need to run a query first to get the count of total rows. Store this count in a class level variable and then when you populate the list, if this count is greater than 100 then enable the next button. I also need to maintain the current page number count. When user presses the Next button, it loads next 100 rows and then check if you still need to enable the Next button by checking if PageNumber*100 is less than row count.

2) A need to enable the Previous button if current page number is not 1.

I would be grateful if someone could help with this or point me in the direction for further help. Many thanks

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

Upvotes: 0

Views: 1291

Answers (1)

VivRichards
VivRichards

Reputation: 17

Is the button not being enabled because you are hitting an exception so are then jumping out of the code? Outside of the try catch could you put something like the follwing

btnNextPostings.Enabled = ListView1.Items.Count > 0

Upvotes: 1

Related Questions