Solved

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

Posted on 2007-04-10
2
977 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

947 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

20 Experts available now in Live!

Get 1:1 Help Now