How to calculate data grid value and textbox value on change in vb.net winform

I want to use calculate data grid value and textbox value on change in vb.net winform. please help me.
Last updated:1/4/2016 12:26:38 PM

1 Answers

Aditya Patel
Aditya Patel

Imports System.Data.SqlClient
Imports System.Globalization
Public Class frmdelivery_challan
    Dim obj As New Delivery_BAL
    Dim objBAL As New Delivery_DAL
    Private EditRow As Integer = -1
    Private Sub gridcolumn()
        Try
            With DataGridView1
                .Columns.Add('rowid', 'rowid')
                .Columns('rowid').Width = 0
                .Columns('rowid').Visible = False
                .Columns.Add('slipno', 'Slip No')
                .Columns('slipno').Width = 150
                .Columns.Add('lotno', 'Lot No')
                .Columns('lotno').Width = 150
                .Columns('lotno').ReadOnly = True
                .Columns.Add('baleno', 'Bale No')
                .Columns('baleno').Width = 150
                .Columns('baleno').ReadOnly = True
                .Columns.Add('qc_cd', 'Quality_code')
                .Columns('qc_cd').Width = 0
                .Columns('qc_cd').ReadOnly = True
                .Columns('qc_cd').Visible = False
                .Columns.Add('Quality', 'Quality')
                .Columns('Quality').Width = 250
                .Columns('Quality').ReadOnly = True
                .Columns.Add('wc_cd', 'Width_code')
                .Columns('wc_cd').Width = 0
                .Columns('wc_cd').Visible = False
                .Columns('wc_cd').ReadOnly = True
                .Columns.Add('Width', 'Width')
                .Columns('Width').Width = 100
                .Columns('Width').ReadOnly = True
                .Columns.Add('bale', 'Bale')
                .Columns('bale').Width = 100
                ''.Columns('bale').ReadOnly = True
                .Columns.Add('Pcs', 'Pcs')
                .Columns('Pcs').Width = 150
                '' .Columns('Pcs').ReadOnly = True
                .Columns.Add('Lmeter', 'Meter')
                .Columns('Lmeter').Width = 150
                ''.Columns('Lmeter').ReadOnly = True
            End With


        Catch ex As Exception

        End Try
    End Sub

    

    Private Sub frmdelivery_challan_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            gridcolumn()
            Desable()
            Last()
        Catch ex As Exception

        End Try
       
    End Sub
    Private Sub txtparty_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtparty.KeyDown
        Try
            If e.KeyCode = Keys.F1 Then
                formname = Me.Name
                frmledger_list.ShowDialog()
            End If
        Catch ex As Exception

        End Try
       
    End Sub
    Private Sub txttransport_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txttransport.KeyDown
        Try
            If e.KeyCode = Keys.F1 Then
                formname = Me.Name
                frmtransport_list.ShowDialog()
            End If
        Catch ex As Exception

        End Try
      
    End Sub
    Private Sub frmdelivery_challan_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Try

            If e.KeyCode = Keys.Enter Then
                SendKeys.Send('{TAB}')
            ElseIf e.Control Then
                If e.KeyCode = Keys.A Then
                    btnadd.PerformClick()
                ElseIf e.KeyCode = Keys.C Then
                    btncancel.PerformClick()
                ElseIf e.KeyCode = Keys.E Then
                    btnedit.PerformClick()
                ElseIf e.KeyCode = Keys.S Then
                    btnupdate.PerformClick()
                ElseIf e.KeyCode = Keys.D Then
                    btndelete.PerformClick()
                ElseIf e.KeyCode = Keys.F Then
                    btnfrist.PerformClick()
                ElseIf e.KeyCode = Keys.N Then
                    btnnext.PerformClick()
                ElseIf e.KeyCode = Keys.P Then
                    btnprevious.PerformClick()
                ElseIf e.KeyCode = Keys.L Then
                    btnlast.PerformClick()
                ElseIf e.KeyCode = Keys.X Then
                    btnclose.PerformClick()
                End If
            ElseIf e.KeyCode = Keys.Escape Then
                Dim result = MessageBox.Show(' Are you sure close This. ', 'Are you sure?', MessageBoxButtons.OKCancel)
                If result = DialogResult.OK Then
                    delrecsqlExists('challan', 'challan_dtl', 'challan', 'challan_dtl', 'chlno', 'totpcs', 'lc_cd', 'totmtr', 0, 0, 0, 0)
                    Me.Close()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Sub cleare()
        Try

            txtchlno.Text = ''
            txtdate.Text = Format(Date.Now, 'dd/MM/yyyy').ToString
            txtdelplace.Text = ''
            txteditid.Text = ''
            txtparty.Text = ''
            txtparty_code.Text = ''
            txttotmeter.Text = 0
            txttotpcs.Text = 0
            txttotbale.Text = 0
            txttransport.Text = ''
            txttransport_code.Text = ''
            txtveh.Text = ''
            DataGridView1.Rows.Clear()
            txtdate.Focus()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub Desable()
        Try

            txtchlno.Focus()
            txtdate.Enabled = False
            txtdelplace.Enabled = False
            txteditid.Enabled = False
            txtparty.Enabled = False
            txtparty_code.Enabled = False
            txttotmeter.Enabled = False
            txttotpcs.Enabled = False
            txttotbale.Enabled = False
            txttransport.Enabled = False
            txttransport_code.Enabled = False
            txtveh.Enabled = False
            DataGridView1.Enabled = False

        Catch ex As Exception

        End Try
    End Sub
    Public Sub Enable()
        Try

            txtdate.Enabled = True
            txtdelplace.Enabled = True
            txteditid.Enabled = True
            txtparty.Enabled = True
            txtparty_code.Enabled = True
            txttotmeter.Enabled = True
            txttotpcs.Enabled = True
            txttotbale.Enabled = True
            txttransport.Enabled = True
            txttransport_code.Enabled = True
            txtveh.Enabled = True
            DataGridView1.Enabled = True 
        Catch ex As Exception

        End Try
    End Sub
    Public Sub Nexts()
        Try

            obj.page = obj.page + 1
            obj.Chlno = ''
            obj.Get_Data(obj)
            i = i + 1
            txtchlno.Text = obj.Chlno
            txtdelplace.Text = obj.delplce
            txtparty.Text = obj.customer
            txtparty_code.Text = obj.customer_code
            txttotmeter.Text = obj.totmtr
            txttotpcs.Text = obj.totpcs
            txttransport.Text = obj.transport
            txttransport_code.Text = obj.transport_code
            txttotbale.Text = obj.totbale
            txtdate.Text = obj.dt
            txtveh.Text = obj.vehno
            If obj.page = 1 Then
                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnnext.Enabled = True
                btnlast.Enabled = True

                btnupdate.Enabled = False
                btncancel.Enabled = False
            ElseIf obj.page = lblrecordno.Text Then
                btnprevious.Enabled = True
                btnfrist.Enabled = True
                btnnext.Enabled = False
                btnlast.Enabled = False

                btnupdate.Enabled = False
                btncancel.Enabled = False
            Else
                btnprevious.Enabled = True
                btnfrist.Enabled = True
                btnnext.Enabled = True
                btnlast.Enabled = True
                btnupdate.Enabled = False
                btncancel.Enabled = False
            End If
            fill_grid_data()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub Back()
        Try

            obj.page = obj.page - 1
            obj.Chlno = ''
            obj.Get_Data(obj)
            txtchlno.Text = obj.Chlno
            txtdelplace.Text = obj.delplce
            txtparty.Text = obj.customer
            txtparty_code.Text = obj.customer_code
            txttotmeter.Text = obj.totmtr
            txttotpcs.Text = obj.totpcs
            txttransport.Text = obj.transport
            txttransport_code.Text = obj.transport_code
            txttotbale.Text = obj.totbale
            txtdate.Text = obj.dt
            txtveh.Text = obj.vehno
            If obj.page = 1 Then
                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnnext.Enabled = True
                btnlast.Enabled = True
                btnupdate.Enabled = False
                btncancel.Enabled = False

            ElseIf obj.page > 1 Then
                btnprevious.Enabled = True
                btnfrist.Enabled = True
                btnnext.Enabled = True
                btnlast.Enabled = True
                btnupdate.Enabled = False
                btncancel.Enabled = False
            End If
            fill_grid_data()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub first()
        Try

            obj.page = 1
            obj.Chlno = ''
            obj.Get_Data(obj)
            txtchlno.Text = obj.Chlno
            txtdelplace.Text = obj.delplce
            txtparty.Text = obj.customer
            txtparty_code.Text = obj.customer_code
            txttotmeter.Text = obj.totmtr
            txttotpcs.Text = obj.totpcs
            txttransport.Text = obj.transport
            txttransport_code.Text = obj.transport_code
            txttotbale.Text = obj.totbale
            txtdate.Text = obj.dt
            txtveh.Text = obj.vehno
            If obj.page = 1 Then
                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnnext.Enabled = True
                btnlast.Enabled = True
                btnupdate.Enabled = False
                btncancel.Enabled = False
            End If
            fill_grid_data()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub Last()
        Try

            obj.page = 0
            obj.Chlno = ''
            obj.Get_Data(obj)
            txtchlno.Text = obj.Chlno
            txtdelplace.Text = obj.delplce
            txtparty.Text = obj.customer
            txtparty_code.Text = obj.customer_code
            txttotmeter.Text = obj.totmtr
            txttotpcs.Text = obj.totpcs
            txttransport.Text = obj.transport
            txttransport_code.Text = obj.transport_code
            txttotbale.Text = obj.totbale
            txtdate.Text = obj.dt
            lblrecordno.Text = obj.page
            txtveh.Text = obj.vehno
            If obj.page > 1 Then
                btnprevious.Enabled = True
                btnfrist.Enabled = True
                btnlast.Enabled = False
                btnnext.Enabled = False
                btnupdate.Enabled = False
                btncancel.Enabled = False
            ElseIf obj.page = 1 Then
                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnnext.Enabled = False
                btnlast.Enabled = False

                btnupdate.Enabled = False
                btncancel.Enabled = False
            End If
            If obj.msg = 'Yes' Then
                btndelete.Enabled = True
                btnedit.Enabled = True

            Else
                btndelete.Enabled = False
                btnedit.Enabled = False
                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnlast.Enabled = False
                btnnext.Enabled = False
                btnupdate.Enabled = False
                btncancel.Enabled = False
                btnadd.Enabled = True
                cleare()
            End If
            fill_grid_data()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub fill_filterdata()
        Try

            obj.page = 0
            obj.Chlno = txtchlno.Text
            obj.Get_Data(obj)
            txtchlno.Text = obj.Chlno
            txtdelplace.Text = obj.delplce
            txtparty.Text = obj.customer
            txtparty_code.Text = obj.customer_code
            txttotmeter.Text = obj.totmtr
            txttotpcs.Text = obj.totpcs
            txttransport.Text = obj.transport
            txttransport_code.Text = obj.transport_code
            txttotbale.Text = obj.totbale
            txtdate.Text = obj.dt
            If obj.page = 1 Then
                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnnext.Enabled = False
                btnlast.Enabled = False
            ElseIf obj.page = Convert.ToInt32(lblrecordno.Text) Then
                btnprevious.Enabled = True
                btnfrist.Enabled = True
                btnnext.Enabled = False
                btnlast.Enabled = False
            Else
                btnprevious.Enabled = True
                btnfrist.Enabled = True
                btnnext.Enabled = True
                btnlast.Enabled = True
            End If
            fill_grid_data()

        Catch ex As Exception

        End Try
    End Sub
    Private Sub fill_grid_data()
        Try

            obj.Get_Grid_Table(obj)
            Dim count As Integer = obj.Get_Details_table.Rows.Count
            Dim index As Integer = 0
            DataGridView1.Rows.Clear()
            Do While index < count
                DataGridView1.Rows.Add(obj.Get_Details_table.Rows(index)('row_id'), obj.Get_Details_table.Rows(index)('p_slipno'), obj.Get_Details_table.Rows(index)('lotno'), obj.Get_Details_table.Rows(index)('baleno'),
                                     obj.Get_Details_table.Rows(index)('qc_cd'), obj.Get_Details_table.Rows(index)('qc_name'), obj.Get_Details_table.Rows(index)('wc_cd'),
                                     obj.Get_Details_table.Rows(index)('wc_name'), obj.Get_Details_table.Rows(index)('bale'),
                                        obj.Get_Details_table.Rows(index)('pcs'), obj.Get_Details_table.Rows(index)('l_mtr'))
                index = index + 1
            Loop

        Catch ex As Exception

        End Try
    End Sub

    Public Sub insert_data()
        Try

            If txtdate.Text = '  /  /' Then
                MsgBox('Please Enter Date')
                txtdate.Focus()
                Exit Sub
            ElseIf Convert.ToDateTime(txtdate.Text) < Convert.ToDateTime(gbstartdate) Then
                MessageBox.Show('Please Enter financial year dates')
                txtdate.Focus()
                Exit Sub
            ElseIf Convert.ToDateTime(gbenddate) < Convert.ToDateTime(txtdate.Text) Then
                MessageBox.Show('Please Enter financial year dates')
                txtdate.Focus()
                Exit Sub
            ElseIf txtparty.Text = '' Then
                MsgBox('Select Party')
                txtparty.Focus()
                Exit Sub
            End If
            obj.Chlno = IIf(IsDBNull(txtchlno.Text), '', txtchlno.Text)
            obj.customer = IIf(IsDBNull(txtparty_code.Text), '', txtparty_code.Text)
            obj.delplce = IIf(IsDBNull(txtdelplace.Text), '', txtdelplace.Text)
            obj.dt = IIf(IsDBNull(Format(Convert.ToDateTime(txtdate.Text), 'yyyy-MM-dd')), 0, Format(Convert.ToDateTime(txtdate.Text), 'yyyy-MM-dd'))
            obj.totbale = IIf(IsDBNull(Convert.ToDecimal(txttotbale.Text)), 0, Convert.ToDecimal(txttotbale.Text))
            obj.transport = IIf(IsDBNull(txttransport_code.Text), '', txttransport_code.Text)
            obj.vehno = IIf(IsDBNull(txtveh.Text), '', txtveh.Text)
            obj.totpcs = IIf(IsDBNull(Convert.ToDecimal(txttotpcs.Text)), 0, Convert.ToDecimal(txttotpcs.Text))
            obj.totmtr = IIf(IsDBNull(Convert.ToDecimal(txttotmeter.Text)), 0, Convert.ToDecimal(txttotmeter.Text))

            objBAL.insert_BROKER(obj)
            insert_grid_Data()
            cleare()
            Last()
            Desable()
            btnadd.Enabled = True
            btncancel.Enabled = False
            btnedit.Enabled = True
            btnclose.Enabled = True
            btndelete.Enabled = True
            btnupdate.Enabled = False
        Catch ex As Exception

        End Try
    End Sub
    Private Sub insert_grid_Data()
        Try

            Dim count As Integer = DataGridView1.Rows.Count
            Dim index As Integer = 0
            delrecsql('challan_dtl', 'chlno', txtchlno.Text, False)
            Do While index < count
                If DataGridView1.Rows(index).Cells('lotno').Value <> Nothing And DataGridView1.Rows(index).Cells('slipno').Value <> Nothing And DataGridView1.Rows(index).Cells('Pcs').Value <> 0 Or Nothing And DataGridView1.Rows(index).Cells('Lmeter').Value <> 0 Or Nothing Then
                    obj.pslip = DataGridView1.Rows(index).Cells('slipno').Value
                    obj.Chlno = txtchlno.Text
                    obj.lotno = DataGridView1.Rows(index).Cells('lotno').Value
                    obj.baleno = DataGridView1.Rows(index).Cells('baleno').Value
                    obj.quality = IIf(IsDBNull(DataGridView1.Rows(index).Cells('qc_cd').Value), '', DataGridView1.Rows(index).Cells('qc_cd').Value)
                    obj.wdth = IIf(IsDBNull(DataGridView1.Rows(index).Cells('wc_cd').Value), '', DataGridView1.Rows(index).Cells('wc_cd').Value)
                    obj.bale = DataGridView1.Rows(index).Cells('Bale').Value
                    obj.pcs = DataGridView1.Rows(index).Cells('Pcs').Value
                    obj.lmtr = DataGridView1.Rows(index).Cells('Lmeter').Value
                    obj.ROW_ID = DataGridView1.Rows(index).Cells('rowid').Value
                    objBAL.insert_Lot_Grid(obj)
                End If
                index = index + 1
            Loop

        Catch ex As Exception

        End Try
    End Sub

    Public Sub search()
        Try
            Dim sqlstr As String = ''
            Dim vailidate As Boolean
            Dim dr As SqlDataReader
            Dim cmd As New SqlCommand
            Dim count As Integer = 0
            Dim newrow As Integer = 0
            If (DataGridView1.CurrentRow.Cells('slipno').Value = '') Then

                Exit Sub
            Else
                vailidate = chackLotnumber()
                If vailidate = False Then
                    Exit Sub
                End If
                sqlstr = 'select * from challan_delivery_bal where slipno='' + DataGridView1.CurrentRow.Cells('slipno').Value.ToString.ToUpper + '' and pcs<>0 and mtr<>0'
                cmd.CommandText = sqlstr
                cmd.Connection = cn
                '' If Not dr.IsClosed Then dr.Close()
                dr = cmd.ExecuteReader
                If dr.HasRows Then
                    Do While dr.Read
                        If count = 0 Then
                            DataGridView1.CurrentRow.Cells('rowid').Value = dr('row_id')
                            DataGridView1.CurrentRow.Cells('slipno').Value = dr('slipno')
                            DataGridView1.CurrentRow.Cells('lotno').Value = dr('lotno')
                            DataGridView1.CurrentRow.Cells('baleno').Value = dr('baleno')
                            DataGridView1.CurrentRow.Cells('qc_cd').Value = dr('qc_cd')
                            DataGridView1.CurrentRow.Cells('quality').Value = dr('qc_name')
                            DataGridView1.CurrentRow.Cells('width').Value = dr('wc_name')
                            DataGridView1.CurrentRow.Cells('wc_cd').Value = dr('wc_cd')
                            DataGridView1.CurrentRow.Cells('Bale').Value = dr('bale')
                            DataGridView1.CurrentRow.Cells('Pcs').Value = dr('pcs')
                            DataGridView1.CurrentRow.Cells('Lmeter').Value = dr('mtr')
                            count = count + 1
                        Else
                            newrow = DataGridView1.Rows.Add
                            DataGridView1.Rows(newrow).Cells('rowid').Value = dr('row_id')
                            DataGridView1.Rows(newrow).Cells('slipno').Value = dr('slipno')
                            DataGridView1.Rows(newrow).Cells('lotno').Value = dr('lotno')
                            DataGridView1.Rows(newrow).Cells('baleno').Value = dr('baleno')
                            DataGridView1.Rows(newrow).Cells('qc_cd').Value = dr('qc_cd')
                            DataGridView1.Rows(newrow).Cells('quality').Value = dr('qc_name')
                            DataGridView1.Rows(newrow).Cells('width').Value = dr('wc_name')
                            DataGridView1.Rows(newrow).Cells('wc_cd').Value = dr('wc_cd')
                            DataGridView1.Rows(newrow).Cells('Bale').Value = dr('bale')
                            DataGridView1.Rows(newrow).Cells('Pcs').Value = dr('pcs')
                            DataGridView1.Rows(newrow).Cells('Lmeter').Value = dr('mtr')
                            count = count + 1
                        End If
                    Loop
                Else
                    MessageBox.Show('Alrady issue Lot number Packged')
                    DataGridView1.CurrentRow.Cells('slipno').Value = ''
                End If
                dr.Close()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub DataGridView1_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Try

            If DataGridView1.Rows.Count > 1 Then
                Dim index As Integer = 0
                Dim pcs As Decimal = 0
                Dim mtr As Decimal = 0
                Dim bale As Decimal = 0
                Select Case e.ColumnIndex
                    Case 1
                        search()
                        calculate()
                        LockCell()
                        insert_grid_Data()
                    Case 8
                        calculate()
                        LockCell()
                        insert_grid_Data()
                    Case 9
                        calculate()
                        LockCell()
                        insert_grid_Data()
                    Case 10
                        calculate()
                        LockCell()
                        insert_grid_Data()
                End Select
            End If

        Catch ex As Exception

        End Try
    End Sub
    Public Function chackLotnumber() As Boolean
        Try

            Dim sqlstr As String = ''
            Dim dr As SqlDataReader
            Dim cmd As New SqlCommand
            If (DataGridView1.CurrentRow.Cells('slipno').Value = '') Then
                Exit Function
            Else
                sqlstr = 'select * from packing where slipno='' & DataGridView1.CurrentRow.Cells('slipno').Value & '''
            End If
            cmd.CommandText = sqlstr
            cmd.Connection = cn
            dr = cmd.ExecuteReader
            If dr.HasRows Then
                dr.Read()
                If txtparty_code.Text <> dr!lc_cd Then
                    MsgBox('Party Not Matching')
                    dr.Close()
                    DataGridView1.CurrentRow.Cells('slipno').Value = ''
                    Return False
                    Exit Function
                Else
                    dr.Close()
                    Return True
                End If
            Else
                MsgBox('Invalid Lot No')
                DataGridView1.CurrentRow.Cells('slipno').Value = ''
                dr.Close()
                Return False
                Exit Function

            End If
            dr.Close()

        Catch ex As Exception

        End Try
    End Function

    Public Sub calculate()
        Try

            Dim index As Integer = 0
            Dim pcs As Decimal = 0
            Dim mtr As Decimal = 0
            Dim bale As Decimal = 0
            Do While index < DataGridView1.Rows.Count - 1
                pcs += IIf(IsDBNull(DataGridView1.Rows(index).Cells('pcs').Value), 0, DataGridView1.Rows(index).Cells('pcs').Value)
                mtr += IIf(IsDBNull(DataGridView1.Rows(index).Cells('Lmeter').Value), 0, DataGridView1.Rows(index).Cells('Lmeter').Value)
                bale += IIf(IsDBNull(DataGridView1.Rows(index).Cells('bale').Value), 0, DataGridView1.Rows(index).Cells('bale').Value)
                index = index + 1
            Loop
            txttotmeter.Text = mtr
            txttotpcs.Text = pcs
            txttotbale.Text = bale

        Catch ex As Exception

        End Try
    End Sub
    Public Sub LockCell()
        Try
           

            If DataGridView1.Rows.Count > 1 Then
                Dim index As Integer = 0
                Do While index < DataGridView1.Rows.Count - 1
                    If DataGridView1.Rows(index).Cells('slipno').Value <> '' Then
                        DataGridView1.Rows(index).Cells('rowid').ReadOnly = True
                        DataGridView1.Rows(index).Cells('slipno').ReadOnly = True
                        DataGridView1.Rows(index).Cells('lotno').ReadOnly = True
                        DataGridView1.Rows(index).Cells('baleno').ReadOnly = True
                        DataGridView1.Rows(index).Cells('qc_cd').ReadOnly = True
                        DataGridView1.Rows(index).Cells('quality').ReadOnly = True
                        DataGridView1.Rows(index).Cells('width').ReadOnly = True
                        DataGridView1.Rows(index).Cells('wc_cd').ReadOnly = True
                        DataGridView1.Rows(index).Cells('Bale').ReadOnly = True
                        DataGridView1.Rows(index).Cells('Pcs').ReadOnly = True
                        DataGridView1.Rows(index).Cells('Lmeter').ReadOnly = True
                    End If
                    index = index + 1
                Loop
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub DataGridView1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
        'If DataGridView1.CurrentCell.ColumnIndex = 1 Or _
        '    DataGridView1.CurrentCell.ColumnIndex = 2 Or _
        '     DataGridView1.CurrentCell.ColumnIndex = 3 Then
        '    AllowOnlyNumeric(e, '.')
        'End If
    End Sub


    Private Sub txtchlno_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtchlno.KeyDown
        Try

            If e.KeyCode = Keys.F1 Then
                formname = Me.Name
                frmdelivery_challanList.ShowDialog()
            End If

        Catch ex As Exception

        End Try
    End Sub
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Try

            EditRow = DataGridView1.CurrentRow.Index
            AddHandler e.Control.KeyPress, AddressOf DataGridView1_KeyPress

        Catch ex As Exception

        End Try
    End Sub
    Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
        Try
            If EditRow >= 0 Then
                Dim newRow As Integer = EditRow
                EditRow = -1
                DataGridView1.CurrentCell = DataGridView1.Rows(newRow).Cells(DataGridView1.CurrentCell.ColumnIndex)
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnadd_Click(sender As System.Object, e As System.EventArgs) Handles btnadd.Click
        Try
            Enable()
            cleare()
            txtchlno.Text = Get_ID('challan', 'chlno', Convert.ToDateTime(gbstartdate).ToString('yy') & '/', False, '', 6)
            insertID('challan', 'chlno', 'lc_cd', 'totpcs', txtchlno.Text, 0, 0)
            btnadd.Enabled = False
            btncancel.Enabled = True
            btnedit.Enabled = False
            btnclose.Enabled = True
            btndelete.Enabled = False
            btnfrist.Enabled = False
            btnlast.Enabled = False
            btnnext.Enabled = False
            btnprevious.Enabled = False
            btnupdate.Enabled = True

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnedit_Click(sender As System.Object, e As System.EventArgs) Handles btnedit.Click
        Try

            If Get_Value('challan', 'status', 'chlno='' + txtchlno.Text + ''') <> 0 Then
                MsgBox('This Entry Can not Edit')
                Exit Sub
            Else
                Enable()
                btnadd.Enabled = False
                btncancel.Enabled = True
                btnedit.Enabled = False
                btnclose.Enabled = True
                btndelete.Enabled = True
                btnupdate.Enabled = True

                btnprevious.Enabled = False
                btnfrist.Enabled = False
                btnlast.Enabled = False
                btnnext.Enabled = False
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btncancel_Click(sender As System.Object, e As System.EventArgs) Handles btncancel.Click
        Try
            btnadd.Enabled = True
            btncancel.Enabled = False
            btnedit.Enabled = True
            btnclose.Enabled = True
            btndelete.Enabled = True
            btnupdate.Enabled = False
            delrecsqlExists('challan', 'challan_dtl', 'challan', 'challan_dtl', 'chlno', 'totpcs', 'lc_cd', 'totmtr', txtchlno.Text, 0, 0, 0)
            cleare()
            Desable()
            Last()

        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnupdate_Click(sender As System.Object, e As System.EventArgs) Handles btnupdate.Click
        Try
            insert_data()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btndelete_Click(sender As System.Object, e As System.EventArgs) Handles btndelete.Click
        Try
            If Get_Value('challan', 'status', 'chlno='' + txtchlno.Text + ''') <> 0 Then
                MsgBox('This Entry Can Not Delete')
                Exit Sub
            Else
                delrecsql('challan', 'chlno', txtchlno.Text, False)
                delrecsql('challan_dtl', 'chlno', txtchlno.Text, False)
                '' update_Value('Packing', 'status', '0', 'slipno='' + txtbillno.Text + ''')
                cleare()
                Desable()
                Last()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnfrist_Click(sender As System.Object, e As System.EventArgs) Handles btnfrist.Click
        Try
            Desable()
            first()
        Catch ex As Exception

        End Try
       
    End Sub

    Private Sub btnnext_Click(sender As System.Object, e As System.EventArgs) Handles btnnext.Click
        Try
            Desable()
            Nexts()
        Catch ex As Exception

        End Try
       
    End Sub

    Private Sub btnprevious_Click(sender As System.Object, e As System.EventArgs) Handles btnprevious.Click
        Try
            Desable()
            Back()
        Catch ex As Exception

        End Try
     
    End Sub

    Private Sub btnlast_Click(sender As System.Object, e As System.EventArgs) Handles btnlast.Click
        Try
            Desable()
            Last()
        Catch ex As Exception

        End Try
      
    End Sub

    Private Sub btnclose_Click(sender As System.Object, e As System.EventArgs) Handles btnclose.Click
        Try
            delrecsqlExists('challan', 'challan_dtl', 'challan', 'challan_dtl', 'chlno', 'totpcs', 'lc_cd', 'totmtr', 0, 0, 0, 0)
            Me.Close()
        Catch ex As Exception

        End Try
     
    End Sub

    Private Sub frmdelivery_challan_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Try
            delrecsqlExists('challan', 'challan_dtl', 'challan', 'challan_dtl', 'chlno', 'totpcs', 'lc_cd', 'totmtr', 0, 0, 0, 0)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub DataGridView1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        Try


            If e.KeyCode = Keys.Return Then
                If DataGridView1.CurrentRow.Index < DataGridView1.RowCount - 1 AndAlso DataGridView1.CurrentCell.ColumnIndex = DataGridView1.ColumnCount - 1 Then
                Else
                    Dim cur_cell As DataGridViewCell = DataGridView1.CurrentCell
                    Dim col As Integer = cur_cell.ColumnIndex
                    col = (col + 1) Mod DataGridView1.Columns.Count
                    cur_cell = DataGridView1.CurrentRow.Cells(col)
                End If
                e.Handled = True
            End If

        Catch ex As Exception
        End Try
    End Sub

    Private Sub DataGridView1_RowEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        Try

            If txtdate.Text = '  /  /' Then
                MsgBox('Please Enter Date')
                txtdate.Focus()
                Exit Sub
            ElseIf txtparty.Text = '' Then
                MsgBox('Select Party')
                txtparty.Focus()
                Exit Sub
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtdate_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles txtdate.Validating
        Try

            Dim vailidate As Boolean
            Dim dt As DateTime
            vailidate = DateTime.TryParseExact(txtdate.Text, 'dd/MM/yyyy', Nothing, DateTimeStyles.None, dt)
            If vailidate = False Then
                MsgBox('Wrong Date')
                txtdate.Focus()
                Exit Sub
            End If
            If Convert.ToDateTime(txtdate.Text) < Convert.ToDateTime(gbstartdate) Then
                MessageBox.Show('Please Enter financial year dates')
                txtdate.Focus()
                Exit Sub
            ElseIf Convert.ToDateTime(gbenddate) < Convert.ToDateTime(txtdate.Text) Then
                MessageBox.Show('Please Enter financial year dates')
                txtdate.Focus()
                Exit Sub
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub DataGridView1_UserDeletingRow(sender As System.Object, e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
        If Not MessageBox.Show('Are you sure Delete?', 'Deleting', MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            e.Cancel = True
        End If
    End Sub

    Private Sub DataGridView1_UserDeletedRow(sender As System.Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserDeletedRow
        Try

            calculate()
            insert_grid_Data()

        Catch ex As Exception

        End Try
    End Sub
    Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtparty.Leave
        Try

        Dim tb As TextBox = DirectCast(sender, TextBox)
        With tb
            Select Case tb.Name
                Case 'txtparty'
                    If .Text = '' Then
                        MsgBox('This field Select From Master Press F1  !!')
                    End If
            End Select
            End With
        Catch ex As Exception

        End Try
    End Sub

    
End Class

Answer