Click here to Skip to main content
14,734,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I got problem here that i cannot find solution i got stuck so i will need little help, maybe improvement in my code if its required to be better.

What i wanted to do was make Buttons Next Previous First Last to select the row index.
And when i click on some cell to display me the information in the textboxes and then if i click Next button to go to next record, or Previous.

My code now do this:
- Select row by buttons was okay
- But if i select now some row random example 3 , it display me the result in textboxes, but if i click the button Next Previous not working correct to diplay the information and row selection.

Here is my project code:
Public Class Form2
    Dim maxRows As Integer
    Dim inc As Integer

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using con As New OleDbConnection(ServerStatus)
            Using cmd As New OleDbCommand("SELECT * FROM Table", con)
                cmd.CommandType = CommandType.Text
                Using sda As New OleDbDataAdapter(cmd)
                    Using dt As New DataTable()
                        sda.Fill(dt)

                        'Set AutoGenerateColumns False
                        DataDisplay.AutoGenerateColumns = False
                        'DataDisplay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                        DataDisplay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                        DataDisplay.AutoResizeColumns()

                        'Set Columns Count
                        DataDisplay.ColumnCount = 6

                        'Add Columns
                        DataDisplay.Columns(0).Name = "ID"
                        DataDisplay.Columns(0).HeaderText = "ID"
                        DataDisplay.Columns(0).DataPropertyName = "ID"

                        DataDisplay.Columns(1).Name = "cName"
                        DataDisplay.Columns(1).HeaderText = "Name"
                        DataDisplay.Columns(1).DataPropertyName = "cName"

                        DataDisplay.Columns(2).Name = "cNumber"
                        DataDisplay.Columns(2).HeaderText = "Number"
                        DataDisplay.Columns(2).DataPropertyName = "cNumber"

                        DataDisplay.Columns(3).Name = "cSupplier"
                        DataDisplay.Columns(3).HeaderText = "Supplier"
                        DataDisplay.Columns(3).DataPropertyName = "cSupplier"

                        DataDisplay.Columns(4).Name = "cStore"
                        DataDisplay.Columns(4).HeaderText = "Store"
                        DataDisplay.Columns(4).DataPropertyName = "cStore"

                        DataDisplay.Columns(5).Name = "cCount"
                        DataDisplay.Columns(5).HeaderText = "Count"
                        DataDisplay.Columns(5).DataPropertyName = "cCount"

                        maxRows = dt.Rows.Count
                        inc = 0

                        TextBox1.Text = dt.Rows(inc).Item(1).ToString
                        TextBox2.Text = dt.Rows(inc).Item(2).ToString
                        TextBox3.Text = dt.Rows(inc).Item(3).ToString
                        TextBox4.Text = dt.Rows(inc).Item(4).ToString
                        TextBox5.Text = dt.Rows(inc).Item(5).ToString
                        TextBox6.Text = dt.Rows(inc).Item(0).ToString

                        DataDisplay.DataSource = dt
                    End Using
                End Using
            End Using
        End Using
        Try
            With cmd
                Dim stream As New IO.MemoryStream()
                conn.Open()
                .Connection = conn
                .CommandText = "select cPicture from Table where ID=@uID"
                .Parameters.Add("@uID", OleDbType.Integer, 50).Value = TextBox6.Text
                Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                stream.Write(image, 0, image.Length)
                Dim bitmap As New Bitmap(stream)
                PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
                Label1.Text = "View Image"
                stream.Close()
                .Parameters.Clear()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try
    End Sub

Private Sub RefreshData()
        Using con As New OleDbConnection(ServerStatus)
            Using cmd As New OleDbCommand("SELECT * FROM Table", con)
                cmd.CommandType = CommandType.Text
                Using sda As New OleDbDataAdapter(cmd)
                    Using dt As New DataTable()
                        sda.Fill(dt)

                        TextBox1.Text = dt.Rows(inc).Item(1).ToString
                        TextBox2.Text = dt.Rows(inc).Item(2).ToString
                        TextBox3.Text = dt.Rows(inc).Item(3).ToString
                        TextBox4.Text = dt.Rows(inc).Item(4).ToString
                        TextBox5.Text = dt.Rows(inc).Item(5).ToString
                        TextBox6.Text = dt.Rows(inc).Item(0).ToString

                        DataDisplay.DataSource = dt
                        Label1.Text = "View Image"
                        'Select row
                        If DataDisplay.RowCount > 0 Then
                            DataDisplay.ClearSelection()
                            DataDisplay.CurrentCell = DataDisplay.Rows(inc).Cells(0)
                            DataDisplay.Rows(inc).Selected = True
                        End If
                    End Using
                End Using
            End Using
        End Using
        Try
            With cmd
                Dim stream As New IO.MemoryStream()
                conn.Open()
                .Connection = conn
                .CommandText = "select cPicture from Table where ID=@uID"
                .Parameters.Add("@uID", OleDbType.Integer, 50).Value = TextBox6.Text
                Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                stream.Write(image, 0, image.Length)
                Dim bitmap As New Bitmap(stream)
                PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
                stream.Close()
                .Parameters.Clear()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try
    End Sub

Private Sub ButtonNext_Click(sender As Object, e As EventArgs) Handles ButtonNext.Click
        If inc <> maxRows - 1 Then
            inc = inc + 1
            RefreshData()
        Else
            MsgBox("No More Rows")
        End If
    End Sub

Private Sub ButtonBack_Click(sender As Object, e As EventArgs) Handles ButtonBack.Click
        If inc > 0 Then
            inc = inc - 1
            RefreshData()
        Else
            MsgBox("First Record")
        End If
    End Sub

Private Sub ButtonFirst_Click(sender As Object, e As EventArgs) Handles ButtonFirst.Click
        inc = 0
        RefreshData()
    End Sub

Private Sub ButtonLast_Click(sender As Object, e As EventArgs) Handles ButtonLast.Click
        inc = maxRows - 1
        RefreshData()
    End Sub

Private Sub DataDisplay_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataDisplay.CellClick
        If e.RowIndex >= 0 Then
            Dim row As DataGridViewRow = Me.DataDisplay.Rows(e.RowIndex)
            TextBox6.Text = row.Cells(0).Value.ToString()
            TextBox1.Text = row.Cells(1).Value.ToString()
            TextBox2.Text = row.Cells(2).Value.ToString()
            TextBox3.Text = row.Cells(3).Value.ToString()
            TextBox4.Text = row.Cells(4).Value.ToString()
            TextBox5.Text = row.Cells(5).Value.ToString()
            'Select row
            Dim intIndex = e.RowIndex
            If inc <> maxRows - 1 Then
                inc = inc + intIndex
            End If
            If DataDisplay.RowCount > 0 Then
                DataDisplay.ClearSelection()
                DataDisplay.CurrentCell = DataDisplay.Rows(inc).Cells(intIndex)
                DataDisplay.Rows(inc).Selected = True
            End If
        End If
    End Sub


What I have tried:

Edited:
- I have already solved this problem
- Re-create the code again
- Use different methods
- New way of buttons next back previous last first
Posted
Updated 12-Jun-20 8:16am
v3
Comments
ZurdoDev 10-Jun-20 15:37pm
   
Sounds like your variable inc may not be set right. Just put a breakpoint in your code and debug it and you'll see what is happening.

1 solution

In addition to ZurdoDev's comment, which is the right way to approach this kind of issue, you may want to disable appropriate buttons, so that you do not have to test for current value in your event handlers.
Private Sub DataDisplay_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataDisplay.SelectedIndexChanged
   Dim index As Integer = DataDisplay.SelectedIndex
   Dim last As Integer = DataDisplay.Items.Count - 1
   ButtonNext.Enabled = ButtonLast.Enabled = index > -1 AndAlso index < last
   ButtonBack.Enabled = ButtonFirst.Enabled = index > 0
End Sub

Private Sub ButtonNext_Click(ByVal sender As Object, Byval e As EventArgs) Handles ButtonNext.Click
   DataDisplay.SelectedIndex += 1
End Sub

Private Sub ButtonLast_Click(ByVal sender As Object, Byval e As EventArgs) Handles ButtonLast.Click
   DataDisplay.SelectedIndex = DataDisplay.Items.Count - 1
End Sub

Private Sub ButtonBack_Click(ByVal sender As Object, Byval e As EventArgs) Handles ButtonBack.Click
   DataDisplay.SelectedIndex -= 1
End Sub

Private Sub ButtonFirst_Click(ByVal sender As Object, Byval e As EventArgs) Handles ButtonFirst.Click
   DataDisplay.SelectedIndex = 0
End Sub

With this, no need to check for current selected index in buttons' event handlers, as buttons will only be enabled if they need to.
   
v4
Comments
diablo22 10-Jun-20 16:47pm
   
so you mean i do not need to use inc at the whole source, hmm but my question now is if i use selectedindexchange. For example if i run program and not touch the datagridview and directly click button next record to show, how does it will read to go next row? sorry for stupid question just need to understand logic :)
phil.o 10-Jun-20 17:22pm
   
If no row is selected, selected index will equal -1. With the code I wrote, no button will be enabled then. You can change the logic to make it whatever you find appropriate.
And, yes, basically I think you do not need inc and maxRows class variables.
diablo22 11-Jun-20 11:02am
   
DataDisplay_SelectedIndexChanged i do not have SelectedIndexChanged only SelectionChanged, Datagridview doesn't has this
diablo22 12-Jun-20 10:37am
   
Anyway i managed to do it another way and rebuild the code total :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900