Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i need add value to DataGridView before add value Check if value already exists if value already exists update value in DataGridView vb.net

What I have tried:

VB
For i = 0 To DataGridView1.Rows.Count.ToString - 1
           If DataGridView1.Rows(i).Cells(0).Value = TextBox1.Text Then
               DataGridView1.Rows(i).Cells(2).Value = DataGridView1.Rows(i).Cells(2).Value + 1
           Else
               Try
                   con = New SqlConnection(cs)
                   con.Open()
                   cmd = New SqlCommand("SELECT ItemID, RTRIM(DishName),'1',Rate from Dish where ItemID like '" & TextBox1.Text & "' order by DishName", con)
                   rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                   While (rdr.Read() = True)
                       DataGridView1.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3))
                       Dim num1 As Double
                       For j As Integer = 0 To DataGridView1.RowCount - 1
                           num1 = Val(DataGridView1.Rows(j).Cells("Qty").Value) * Val(DataGridView1.Rows(j).Cells("Rate").Value)
                           num1 = Math.Round(num1, 2)
                           DataGridView1.Rows(j).Cells("Amount").Value = num1
                       Next
                       TotalCalc()
                   End While
                   con.Close()
               Catch ex As Exception
                   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
               End Try
           End If
       Next
Posted
Updated 20-Mar-22 23:44pm
v3
Comments
CHill60 21-Mar-22 5:57am    
And what is wrong with the code you already have?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900