Results 1 to 6 of 6

Thread: [RESOLVED] Issue with DataGridView RowValidating() event.

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Resolved [RESOLVED] Issue with DataGridView RowValidating() event.

    Hello!

    I'm using windows form app, I have a DGV, It calculates the new price when I enter a value in the discount column, it is working fine, the problem is that, when I go up & down thru the DGV using Arrow keys, it keeps changing the price column even though I don't change any value on the discount column.
    Here's an animated gif to show you the issue.
    Name:  GRIDVIEW..jpg
Views: 152
Size:  20.7 KB

    My code is this.
    vb.net Code:
    1. Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
    2.         With DataGridViewX1
    3.             Dim RowX As DataGridViewRow
    4.             RowX = .Rows(e.RowIndex)
    5.  
    6.             Dim Price As Decimal = RowX.Cells(2).Value
    7.             Dim Discount As Decimal = RowX.Cells(5).Value
    8.             Dim Disc_Sustract As Decimal = 1 - (Discount / 100)
    9.  
    10.             RowX.Cells(2).Value = FormatCurrency(Price * Disc_Sustract).ToString
    11.         End With
    12.     End Sub

    I'll appreciate any help.
    Thank you.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Issue with DataGridView RowValidating() event.

    You are calculating the amount to subtract based on the currently displayed price rather than the original price, so if that code is executed multiple times, you are going to keep discounting the current price over and over. You should basing your calculation on the original price rather than the current price, which already includes the discount.

    Also, you shouldn't be formatting in code like that. You should set the DefaultCellStyle.Format for the column to "C" for currency and then put the actual numbers in the cells.

    You could actually do this with no code if you were to use a DataTable and data binding, which really ought to be your first choice. You could have a column for the original price and a column for the discount, then a column for the final price with its Expression property set to perform the calculation. You can then bind that DataTable to the grid and everything will work with no extra VB code.

    Finally, RowValidating is the wrong event to be handling there. The point of that event is to validate the row, which you aren't doing. The RowValidating, RowValidated and RowLeave events in the DataGridView work for a row in exactly the same way as the Validating, Validated and Leave events for a control. You handle Validating if you want to validate the content and prevent the control losing focus if it fails. You handle Validated if you want to do something only on those occasions that validation passes on Validating. You handle Leave when you want to something every time the user leaves the control, regardless of validation. In your case, you're not doing any validation at all, so you should be handling RowLeave. Of course, if you bind a DataTable then you don't need any code at all.

  3. #3

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Issue with DataGridView RowValidating() event.

    Wow!!
    What a piece of advice!
    Yes, my logic was wrong, I should have used the original price from the beginning.
    also I used the Format(Cell(X), "c"), and I switch to RowLeave event. Now everything woks fine.
    regarding of using a Datatable, I don't use it becase I don't need it for this particular case.
    Thank very much.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Issue with DataGridView RowValidating() event.

    Quote Originally Posted by Spybot View Post
    also I used the Format(Cell(X), "c")
    Again, you shouldn't be formatting code like that. That means that your cells contain text rather than numbers. You should be putting numbers into the cells and letting the grid take care of the formatting.
    Quote Originally Posted by Spybot View Post
    regarding of using a Datatable, I don't use it becase I don't need it for this particular case.
    The fact that you don't specifically need it doesn't mean that it's not still a better option. It's your choice though.

  5. #5

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Issue with DataGridView RowValidating() event.

    Ok, you mean click my DGV ..

    -Go to Edit columns
    -Select my column
    -Click on: DefaultCellStyle
    -Click on format
    -select "C2"

    Done!

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Issue with DataGridView RowValidating() event.

    Quote Originally Posted by Spybot View Post
    Ok, you mean click my DGV ..

    -Go to Edit columns
    -Select my column
    -Click on: DefaultCellStyle
    -Click on format
    -select "C2"

    Done!
    Yeah. I should have said "in the designer" to make the intent clearer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width