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)
DataDisplay.AutoGenerateColumns = False
DataDisplay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
DataDisplay.AutoResizeColumns()
DataDisplay.ColumnCount = 6
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
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"
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
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()
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