Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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