Can you help me to add Next and Previous Buttons to this code please:

Imports System.Data.SqlClient

Public Class Form1
    Dim id As Integer = 0
    Dim idindex = 0
    Dim command As SqlCommand
    Dim adapter As SqlDataAdapter
    Dim builder As SqlCommandBuilder
    Dim ds As DataSet
    Dim table As DataTable
    Dim con As SqlConnection = New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=order;Integrated Security=True;Pooling=False")


    Private Sub fillGrd()
        Try
            con.Open()

            Dim sql As String
            sql = "SELECT * FROM orderdetail"
            command = New SqlCommand(sql, con)
            adapter = New SqlDataAdapter(command)
            builder = New SqlCommandBuilder(adapter)
            ds = New DataSet()
            adapter.Fill(ds, "orderindex")
            table = ds.Tables("orderindex")
            con.Close()
            grdIndex.DataSource = ds.Tables("orderindex")
            grdIndex.ReadOnly = True
            grdIndex.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Try
            Dim result As Boolean = False
            Dim str As String
            Dim _IDIndex As Integer
            Dim command As SqlCommand
            If txtnumber.Text = "" Then
                id = 0
            Else
                id = CInt(txtnumber.Text)
            End If
            If id = 0 Then
                str = " insert into [orderindex] ( orderdate,reference,clientname, note) values ( '" & dtdate.Value.ToString("MM/dd/yyyy") & "', '" & txtrefernce.Text & "'," & cmbclient.SelectedValue & ",  '" & txtnote.Text & "');select idindex from orderindex where idindex=@@IDENTITY"
                con.Open()
                command = New SqlCommand(str, con)
                'result = command.ExecuteNonQuery()
                _IDIndex = CInt(command.ExecuteScalar)
                txtnumber.Text = _IDIndex

                If grdIndex.Rows.Count > 1 Then
                    For i = 0 To grdIndex.Rows.Count - 2
                        str = "insert into [orderdetail] (idindex,idproduct,quantity,price) values (" & _IDIndex & "," & grdIndex.Rows(i).Cells("colProduct").Value & "," & grdIndex.Rows(i).Cells("colquantity").Value & "," & grdIndex.Rows(i).Cells("colprice").Value & "  )"
                        command = New SqlCommand(str, con)
                        command.ExecuteNonQuery()
                    Next
                End If
            Else
                str = "update [orderindex] set  orderdate='" & dtdate.Value.ToString("MM/dd/yyyy") & "' , reference= '" & txtrefernce.Text & "' , clientname= " & cmbclient.SelectedValue & ", note= '" & txtnote.Text & "' where idindex=" & id
                command = New SqlCommand(str, con)
                command.ExecuteNonQuery()
                str = "delete from orderdetail where idindex=" & id
                command = New SqlCommand(str, con)
                command.ExecuteNonQuery()
                If grdIndex.Rows.Count > 1 Then
                    For i = 0 To grdIndex.Rows.Count - 2
                        str = "insert into [orderdetail] (idindex,idproduct,quantity,price) values (" & _IDIndex & "," & grdIndex.Rows(i).Cells("colProduct").Value & "," & grdIndex.Rows(i).Cells("colquantity").Value & "," & grdIndex.Rows(i).Cells("colprice").Value & "  )"
                        command = New SqlCommand(str, con)
                        command.ExecuteNonQuery()

                    Next
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim str As String
        If txtnumber.Text <> "" Then
            id = CInt(txtnumber.Text)
        Else
            Exit Sub
        End If
        If MessageBox.Show("do u want to delete this row?", "delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then

            str = "delete from orderdetail where idindex=" & id
            command = New SqlCommand(str, con)
            command.ExecuteNonQuery()
            str = "delete from orderindex where idindex=" & id
            command = New SqlCommand(str, con)
            command.ExecuteNonQuery()

        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fillcombo()
    End Sub

    Private Sub fillcombo()
        Try
            con.Open()

            Dim sql As String
            sql = "SELECT * FROM client"
            command = New SqlCommand(sql, con)
            adapter = New SqlDataAdapter(command)
            builder = New SqlCommandBuilder(adapter)
            Dim _dt As New DataTable
            adapter.Fill(_dt)

            con.Close()
            cmbclient.DataSource = _dt
            cmbclient.DisplayMember = "nomclient"
            cmbclient.ValueMember = "idclient"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



    Private Sub grdIndex_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdIndex.CellEndEdit
        Dim NbQtity As Double = 0
        If e.ColumnIndex = 2 Then
            For i As Integer = 0 To grdIndex.Rows.Count - 2
                NbQtity = NbQtity + grdIndex.Rows(i).Cells("colquantity").Value
            Next
            txtTotalprice.Text = NbQtity
        End If
        Dim nbprice As Double = 0
        If e.ColumnIndex = 3 Then
            For i As Integer = 0 To grdIndex.Rows.Count - 2
                nbprice = nbprice + grdIndex.Rows(i).Cells("colprice").Value
            Next
            txtTotalprice.Text = nbprice
        End If

    End Sub


    Private Sub grdIndex_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles grdIndex.RowsRemoved
        Dim NbQtity As Double = 0
        'If grdIndex.Rows.Count < 2 Then Exit Sub

        For i As Integer = 0 To grdIndex.Rows.Count - 2
            NbQtity = NbQtity + grdIndex.Rows(i).Cells("colquantity").Value
        Next
        txtTotalprice.Text = NbQtity
        Dim nbprice As Double = 0
        'If grdIndex.Rows.Count < 2 Then Exit Sub

        For i As Integer = 0 To grdIndex.Rows.Count - 2
            nbprice = nbprice + grdIndex.Rows(i).Cells("colprice").Value
        Next
        txtTotalprice.Text = nbprice

    End Sub
End Class

What are you trying to achieve here? Navigate to the next/previous row in the datagrid or next/previous cell?

This article has been dead for over six months. Start a new discussion instead.