Solved

URGENT :DataGridView: Update cell content (cell_1) when editing Cell_2, and viceversa.

Posted on 2007-04-10
2
976 Views
Last Modified: 2008-01-09
Hello Experts :

I want to do the following :

I have a datagridview with manual bind to Db, and there are two columns that can be modified. My problem is that I want to update the column_one if column_Two is modified, and I want to update column_Two if user is editing column_one. How to do this ? I ve tried this code and doesnt works :

Private Sub DGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGridView.CellValueChanged
        Dim NomTaula As String = "T_Linies" & Calendari.SelectionRange.Start.Year.ToString

        If e.RowIndex <> -1 Then
            Select Case e.ColumnIndex
               
                Case 1   'column_one
                   
                    If IsNumeric(DGridView.CurrentCell.Value) Then
                        DGridView.CurrentRow.Cells(2).Value = Math.Round((Integer.Parse(DGridView.CurrentRow.Cells(1).Value, Globalization.NumberStyles.Integer) * 100) / DGridView.CurrentRow.Cells(3).Value, 0)
                    End If
                Case 2   ' Column_two

                    If IsNumeric(Integer.Parse(DGridView.CurrentCell.Value, Globalization.NumberStyles.Integer)) Then 'And DGridView.CurrentCell.Style.BackColor = Color.Orange Then
                        DGridView.CurrentRow.Cells(1).Value = Math.Round((CInt(DGridView.CurrentRow.Cells(3).Value) * CInt(DGridView.CurrentRow.Cells(5).Value)) / 100, 0)
                    End If
            End Select
        End If

    End Sub


This code DOES NOT works because the application enters on a bucle, if I modify the column_one, the column_two is updated but the event is fiered again (because column_one has been modifed...)


Any idea please ?  ?
0
Comment
Question by:franklinan
2 Comments
 
LVL 12

Accepted Solution

by:
Mamtha1982 earned 500 total points
ID: 18887685
you are right, the event will fire again and again. to stop this you can cancel firing the event using RemoveHadler and AddHandler ,
so try the following code..

Private Sub DGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGridView.CellValueChanged
        Dim NomTaula As String = "T_Linies" & Calendari.SelectionRange.Start.Year.ToString

        If e.RowIndex <> -1 Then
            Select Case e.ColumnIndex
               
                Case 1   'column_one
                   
                    If IsNumeric(DGridView.CurrentCell.Value) Then

RemoveHandler DGridView.CellValueChanged, AddressOf DGridView_CellValueChanged

                        DGridView.CurrentRow.Cells(2).Value = Math.Round((Integer.Parse(DGridView.CurrentRow.Cells(1).Value, Globalization.NumberStyles.Integer) * 100) / DGridView.CurrentRow.Cells(3).Value, 0)

AddHandler DGridView.CellValueChanged, AddressOf DGridView_CellValueChanged

                    End If
                Case 2   ' Column_two

                    If IsNumeric(Integer.Parse(DGridView.CurrentCell.Value, Globalization.NumberStyles.Integer)) Then 'And DGridView.CurrentCell.Style.BackColor = Color.Orange Then

RemoveHandler DGridView.CellValueChanged, AddressOf DGridView_CellValueChanged

                        DGridView.CurrentRow.Cells(1).Value = Math.Round((CInt(DGridView.CurrentRow.Cells(3).Value) * CInt(DGridView.CurrentRow.Cells(5).Value)) / 100, 0)

AddHandler DGridView.CellValueChanged, AddressOf DGridView_CellValueChanged

                    End If
            End Select
        End If

    End Sub
0
 
LVL 1

Author Comment

by:franklinan
ID: 18895792
Thnaks a lot !! Sorry for my delay in answer...Great !
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 Error 7 55
Encryption Decryption in Oracle 12 61
Tabcontrol Caption? 1 24
Iterate a dictionnary to change values 4 28
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now