Results 1 to 7 of 7

Thread: help with looping through datagridview rows and update sales rate based on value

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    help with looping through datagridview rows and update sales rate based on value

    hi guys, so I've been tinkering with this code for about 2 weeks and keep ending up with the same result where the values don't update until a new row is added in the datagridview. so basically what im trying to do is loop through the rows and update the sales rate based on 2 conditions. 1 condition is a value from a column and the 2nd condition is from a text label. here is my code so far any help would be greatly appreciated.

    Code:
    Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            Try
                If DataGridView1.Rows.Count > 0 Then
                    If txtTaxType.Text = "Inclusive" Then
                        con = New SqlConnection(cs)
                        con.Open()
                        Dim ct As String = "select PRate,PQty,BottleDeposit from product where PID=@d1 and POffer='Yes'"
                        cmd = New SqlCommand(ct)
                        cmd.Parameters.AddWithValue("@d1", Val(DataGridView1.Rows(e.RowIndex).Cells("PID").Value))
                        cmd.Connection = con
                        rdr = cmd.ExecuteReader()
                        If rdr.Read() Then
                            Dim BValue = rdr.GetValue(2)
                            Dim PromoRate = rdr.GetValue(0)
                            Dim PromoQty = Val(rdr.GetValue(1))
                            For Each row As DataGridViewRow In DataGridView1.Rows
                                If Val(DataGridView1.Rows(e.RowIndex).Cells("Qty").Value) >= PromoQty Then
                                    DataGridView1.Rows(e.RowIndex).Cells("SalesRate").Value = PromoRate
                                End If
                                If lblpromo.Text >= 0.2 And Val(DataGridView1.Rows(e.RowIndex).Cells("BD").Value) = BValue Then
                                    row.Cells("SalesRate").Value = PromoRate
                                End If
                            Next
                        End If
                        con.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    Last edited by iced8out; Nov 14th, 2022 at 08:30 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: help with looping through gridview rows and update sales rate based on other colu

    Please use the proper names for things. A DataGridView is a DataGridView, not a GridView. A GridView is something else. A DataGridView is a Windows Forms control while a GridView is a Web Forms control. I don't do Web Forms so I almost didn't even open your question because I thought that it probably wasn't relevant to me. If you use the actual names of things then you won't cause confusion like that and you won't turn people away who might help you.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: help with looping through gridview rows and update sales rate based on other colu

    As for the issue, there's a lot of code there and I would guess that most of it is irrelevant to your problem. You should avoid posting irrelevant code because just obfuscates the problem. It's not always to determine exactly what's relevant and what's not but I'm confident that you can trim it down a bit from that.

    Even better, don't use your actual project at all. When you encounter an issue like this - especially ones that you're at for a while and can't solve - the best course of action is to create a new test project and strip it down to the absolute basics so that you can test your specific issue in isolation. The less you have cluttering the code, the easier it is to see what's actually going on and what the actual problem is. If you've been at this for two weeks, you should have done that long before now and you should be able to show us that test code, which includes everything needed to demonstrate this problem and nothing else. You can even simplify the parameters of the problem, e.g. use fewer columns and fewer conditions.

    Have you actually debugged this code? That doesn't mean just running the project in the debugger. It means actually using the debugger to find the issue, i.e. set a breakpoint at the top of the code, step through the code line by line, examine the state at each step and compare what actually happens to your expectations. You should be able to tell us EXACTLY where and how the behaviour of the code differs from what you expect. I suspect that you haven't done that. If you don't know how to debug, you should stop what you're doing and learn now - there are tutorials around. Debugging is a critical skill for all developers, regardless of experience level. It should be the first thing anyone learns but, sadly, it is often neglected completely, even by paid courses.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: help with looping through gridview rows and update sales rate based on other colu

    Quote Originally Posted by jmcilhinney View Post
    Please use the proper names for things. A DataGridView is a DataGridView, not a GridView. A GridView is something else. A DataGridView is a Windows Forms control while a GridView is a Web Forms control. I don't do Web Forms so I almost didn't even open your question because I thought that it probably wasn't relevant to me. If you use the actual names of things then you won't cause confusion like that and you won't turn people away who might help you.
    Thank you i will update the post.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: help with looping through datagridview rows and update sales rate based on value

    And remove the Try/Catch.
    Then Step through your code line by line checking all values.

    Pretty sure you'll find what's "bugging" you
    I see at least two things looking fishy to me

    EDIT: And it would help, if you could make a "This is what it looks, and this is what it should look after the change"
    Like do it by hand in Excel and post it here
    Last edited by Zvoni; Nov 15th, 2022 at 02:01 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: help with looping through datagridview rows and update sales rate based on value

    Quote Originally Posted by Zvoni View Post
    And remove the Try/Catch.
    Then Step through your code line by line checking all values.

    Pretty sure you'll find what's "bugging" you
    I see at least two things looking fishy to me

    EDIT: And it would help, if you could make a "This is what it looks, and this is what it should look after the change"
    Like do it by hand in Excel and post it here
    Hi Zvoni, thank you for your reply. I removed the Try/Catch and no errors were shown during debugging and had the same results with the Try/Catch There. My code runs But it only fires the updated values after a new row is added to the DataGridView. What im hoping to happen is that the rows get updated with the values without having to add a new row to the DataGridView.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: help with looping through datagridview rows and update sales rate based on value

    Again, it sounds like your just running the project and not doing any actual debugging. Did you set a breakpoint and step through the code? If so, EXACTLY where and how did the behaviour of the code (not the application but the code) differ from your expectations? If not, that's what you need to do.

Tags for this Thread

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