Replication in datagridview (vb.net and ms-access database)

0

When I'm making a new sale (in the case of this software I'm developing), every time I add new items in the datagridview it appears along with previously added items. I would like to clean the datagridview before adding new items (or making a new sale).

I'm using the code below, but it's not working.

Private Sub lvTable_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvTable.ItemChecked
    Try

        If lvTable.CheckedItems.Count > 0 Then
            Dim Condition As String = ""
            Dim Condition1 As String = ""
            For I = 0 To lvTable.CheckedItems.Count - 1

                Condition += String.Format("'{0}',", lvTable.CheckedItems(I).Text)
                Condition1 += String.Format("'{0}',", lvTable.CheckedItems(I).SubItems(1).Text)
            Next
            Condition = Condition.Substring(0, Condition.Length - 1)
            Condition1 = Condition1.Substring(0, Condition1.Length - 1)
            DataGridView2.Visible = True
            con = New OleDbConnection(cs)
            con.Open()
            Dim OleDb As String = "Select Item_ID,DishName, SUM(Qty), KOTGenerationItems.Rate, SUM(Amount), DiscPer, SUM(Disc), VATPer, SUM(VATAmt), STPer, SUM(STAmt), SCPer, SUM(SCAmt), SUM(TotalAmt),TableNo,GroupName from KOTGeneration,KOTGenerationItems,Dish where KOTGeneration.TicketID=KOTGenerationItems.Ticket_ID and KOTGenerationItems.Item_ID=Dish.ItemID and TableNo in (" & Condition & ") and GroupName in (" & Condition1 & ")   group by Item_ID,DishName,KOTGenerationItems.Rate,DiscPer,VATPer,STPer,SCPer,TableNo,GroupName order by TableNo"
            cmd = New OleDbCommand(OleDb, con)
            rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            DataGridView2.Rows.Clear()
            While (rdr.Read() = True)
                DataGridView2.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7), rdr(8), rdr(9), rdr(10), rdr(11), rdr(12), rdr(13), rdr(14), rdr(15))
            End While
            con.Close()
            txtCash.Text = "0.00"
            TotalCalc1()
            Calc()
        Else
            Clear()
            DataGridView2.Rows.Clear()
            DataGridView2.Visible = False
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
    
asked by anonymous 12.12.2017 / 09:26

1 answer

0

Since you are directly adding rows in DataGridView , you must call these two methods in sequence to remove all rows present in the object.

DataGridView2.Rows.Clear()
DataGridView2.Refresh()

If you were filling in with the DataSource property, you would use the following syntax:

DataGridView2.DataSource = Nothing
DataGridView2.Refresh()
    
12.12.2017 / 11:31