I have two Form One is CASH BOOk ANd another is CashBOOkUpdation...............whne i Doubleclick on ny row of datagrid of CashBOOk i go to the 2nd formCashBookUpdation for updating anything.............BT PRoblem is that when i came back to form CashBook after Editing............the Grid SHow The OLD records not the New Updation...........How this Problem Can Be Solved .......So that I Can Get update Data .............PLz anyOne Help me As soon as possible

Recommended Answers

All 5 Replies

You haven't provided any code so it's difficult to know how you are transferring information between the forms or if you are transfering information between the forms.

Also what controls are on CashBookUpdation? What is supposed to cause the update to happen? Click on a button?

A screen capture would be beneficial.

What is your data source?

Use an event and a delegate to notify the main form (CashBook) that an update has occured.

Create a new class and name it: ValueUpdatedEventArgs.vb

ValueUpdatedEventArgs.vb

Imports System

Public Class ValueUpdatedEventArgs
    Inherits System.EventArgs


    'forwards calls to appropriate event handler
    Public Delegate Sub ValueUpdatedEventHandler(ByVal sender As Object, ByVal e As ValueUpdatedEventArgs)

    'constructor
    Public Sub New()

    End Sub

End Class

In the child form (CashBookUpdation) we want to raise an event which the main form (CashBook) is listening for.

CashBookUpdation.vb

Public Class CashBookUpdation
    'Event interested parties can register with to know
    'when value is updated.
    'ValueUpdatedEventHandler is delegate defined in ValueUpdatedEventArgs

    Public Event ValueUpdated As ValueUpdatedEventArgs.ValueUpdatedEventHandler


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SendNotification()
    End Sub

    Private Sub SendNotification()

        'create a new instance of ValueUpdatedEventArgs
        Dim valueArgs As ValueUpdatedEventArgs
        valueArgs = New ValueUpdatedEventArgs()

        'raise event "ValueUpdated"
        'Me refers to this class
        'valueArgs is a new instance of "ValueUpdatedEventArgs"
        'and contains our schedule information
        'we want to pass to the main form (CashBook)

        RaiseEvent ValueUpdated(Me, valueArgs)

    End Sub

End Class

In our main form (CashBook), we need to add a handler (or listener) that performs an action whenever the event is raised in CashBookUpdation.

I know that you are using a DataGridView, but I used a button on the main form for demonstration purposes. Place the code that is inside "Button1_Click" below, in the event handler for your DataGridView (DataGridView1_CellDoubleClick or whatever one you are using).

CashBook.vb

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'create instance of CashBookUpdation
        Dim myCashBookUpdation As CashBookUpdation

        'create new instance of CashBookUpdation
        myCashBookUpdation = New CashBookUpdation()

        'add listener / event handler for myChildFrm1.ValueUpdated event
        AddHandler myCashBookUpdation.ValueUpdated, AddressOf myCashBookUpdation_ValueUpdated

        'show the form
        myCashBookUpdation.Show()

    End Sub

    'event handler
    'this is called every time myCashBookUpdation.ValueUpdated event occurs
    Private Sub myCashBookUpdation_ValueUpdated(ByVal sender As Object, ByVal e As ValueUpdatedEventArgs)

        'I just do a writeline here
        'you will want to update your datagridview
        Console.WriteLine("CashBookUpdation value was updated.")
    End Sub

Note: You could pass back the updated data from the child form (CashBookUpdation) to the main form (CashBook) by adding another constructor and some variables/properties to ValueUpdatedEventArgs.vb

Here is a version that passes data from the child form (CashBookUpdation) to the main form (CashBook). We will use the same classes as above, but will add to it. Also, I renamed "SendNotification" to "SendUpdates" in CashBookUpdation.

Additionally, for demonstration purposes I added two textboxes to CashBookUpdation.vb.

I added Property "Name" and "Amount" to ValueUpdatedEventArgs.vb as well as a constructor that can be used to set the values for them.

ValueUpdatedEventArgs.vb

Imports System

Public Class ValueUpdatedEventArgs
    Inherits System.EventArgs


    'forwards calls to appropriate event handler
    Public Delegate Sub ValueUpdatedEventHandler(ByVal sender As Object, ByVal e As ValueUpdatedEventArgs)

    Private _name As String
    Private _amount As Double

    'constructor
    Public Sub New()

    End Sub

    'constructor
    Public Sub New(ByVal Name As String, _
                    ByVal Amount As Double)

        _name = Name
        _amount = Amount

    End Sub

    Public Property Name As String
        Get
            Return _name
        End Get

        Set(value As String)
            _name = value
        End Set
    End Property 'Name

    Public Property Amount As Double
        Get
            Return _amount
        End Get

        Set(value As Double)
            _amount = value
        End Set
    End Property 'Amount

End Class

CashBookUpdation is mostly the same. The main difference is that instead of using this constructor:

valueArgs = New ValueUpdatedEventArgs()

I am using this one:

valueArgs = New ValueUpdatedEventArgs(name, amount)

CashBookUpdation.vb

Public Class CashBookUpdation
    'Event interested parties can register with to know
    'when value is updated.
    'ValueUpdatedEventHandler is delegate defined in ValueUpdatedEventArgs

    Public Event ValueUpdated As ValueUpdatedEventArgs.ValueUpdatedEventHandler


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

        Dim name As String = TextBox1.Text
        Dim amount As Double = Convert.ToDouble(TextBox2.Text)

        SendUpdates(name, amount)
    End Sub

    Private Sub SendUpdates(ByVal name As String, ByVal amount As Double)

        'create a new instance of ValueUpdatedEventArgs
        'pass our data using the constructor
        Dim valueArgs As ValueUpdatedEventArgs
        valueArgs = New ValueUpdatedEventArgs(name, amount)

        'raise event "ValueUpdated"
        'Me refers to this class
        'valueArgs is a new instance of "ValueUpdatedEventArgs"
        'and contains our schedule information
        'we want to pass to the main form (CashBook)

        RaiseEvent ValueUpdated(Me, valueArgs)

    End Sub

End Class

CashBook.vb is mostly unchanged. In "myCashBookUpdation_ValueUpdated", "e" is an instance of "ValueUpdatedEventArgs". So to access property "Name", we use: e.Name. And to access property "Amount", we use: e.Amount.

CashBook.vb

Public Class CashBook



    Private Sub CashBook_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'create instance of CashBookUpdation
        Dim myCashBookUpdation As CashBookUpdation

        'create new instance of childFrm1
        myCashBookUpdation = New CashBookUpdation()

        'add listener / event handler for myCashBookUpdation.ValueUpdated event
        AddHandler myCashBookUpdation.ValueUpdated, AddressOf myCashBookUpdation_ValueUpdated

        'show the form
        myCashBookUpdation.Show()

    End Sub

    'event handler
    'this is called every time myCashBookUpdation.ValueUpdated event occurs
    Private Sub myCashBookUpdation_ValueUpdated(ByVal sender As Object, ByVal e As ValueUpdatedEventArgs)

        'I just do a writeline here
        'you will want to update your datagridview
        Console.WriteLine("CashBookUpdation value was updated.")

        'our updated data is in "e" which is an instance of
        'ValueUpdatedEventArgs. To access the property
        'Name: e.Name
        Console.WriteLine("name: " & e.Name & " amount: " & e.Amount)
    End Sub

End Class

I dont Want teh Updation on Button Click.........I just Want That when i edit any column of datagrid of cashupdation and close this form.............then i want to see the edited data without cliking on any button.........

Private Sub dgCashBook_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgCashBook.CellDoubleClick

        Dim CashVoucher As New CashVoucher

        bkVoucherDate = DgCashBook.CurrentRow.Cells(0).Value
        bkCashAccountCode = DgCashBook.CurrentRow.Cells(9).Value
        bkVoucherNo = DgCashBook.CurrentRow.Cells(11).Value
        bkVoucherType = DgCashBook.CurrentRow.Cells(12).Value

        bkDivisionCode = DgCashBook.CurrentRow.Cells(13).Value

        Select Case bkVoucherType
            Case "Cash"
                CashVoucher.DgCash.Rows.Clear()
                CashVoucher.txtTotalCredit.Clear()
                CashVoucher.txtTotalDebit.Clear()

                qry = "SELECT * FROM tbCash where CashActCode = " & bkCashAccountCode & " and Voucherdate = # " & bkVoucherDate & " # and VoucherNo = " & bkVoucherNo & ""
                ds = New DataSet
                ad = New OleDbDataAdapter(qry, cn)
                ad.Fill(ds, "tbCash")
                Dim i As Integer = 0
                For i = 0 To ds.Tables("tbCash").Rows.Count() - 1

                    CashVoucher.DgCash.Rows().Add(New String() {
                        ds.Tables("tbCash").Rows(i).Item("ActName").ToString,
                        ds.Tables("tbCash").Rows(i).Item("Type").ToString(),
                        ds.Tables("tbCash").Rows(i).Item("Narration").ToString(),
                        String.Format("{0:f2}", Val(ds.Tables("tbCash").Rows(i).Item("Amount").ToString())),
                        ds.Tables("tbCash").Rows(i).Item("ActCode").ToString()})

                    Select Case ds.Tables("tbCash").Rows(i).Item("Type").ToString
                        Case Is = "Dr"
                            CashVoucher.txtTotalDebit.Text = String.Format("{0:f2}", Val(CashVoucher.txtTotalDebit.Text) + Val(ds.Tables("tbCash").Rows(i).Item("Amount").ToString()))
                        Case Is = "Cr"
                            CashVoucher.txtTotalCredit.Text = String.Format("{0:f2}", Val(CashVoucher.txtTotalCredit.Text) + Val(ds.Tables("tbCash").Rows(i).Item("Amount").ToString()))
                    End Select
                Next i
                DgCashBook.ReadOnly = True
                CashVoucher.dtVoucherDate.Value = bkVoucherDate
                bktype = 1

                CashVoucher.DgCash.Enabled = True
                CashVoucher.TopMost = True
                CashVoucher.Show()
                CashVoucher.DgCash.Focus()

            Case "J Form"
                bktype = 1
                JFormVoucher.Show()
        End Select

THis is the code ..........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.