Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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