Results 1 to 7 of 7

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

Threaded View

  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.

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