Replica in datagridview (Vb.net and ms-access database)

Asked

Viewed 62 times

0

When I am making a new sale (in the case of this software I am developing), every time I add new items in the datagridview it appears together with items (or sales) previously added. I would like to clean the datagridview before adding new items(or make 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

1 answer

0

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

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

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

DataGridView2.DataSource = Nothing
DataGridView2.Refresh()
  • Alo @Gabriel, thanks for your reply, however even putting the methods Datagridview2.Rows.Clear() Datagridview2.Refresh() still keeping to previous lines, some other suggestion??

  • Your select is not returning old lines together?

  • Looking straight into my database in the Kotgenerationitems and Kotgeneration tables which is where SELECT search seems to be all normal.

  • No idea????

  • The method to clean one DataGridView is as I explained in the answer... If your select is returning the correct values and you are using the code I showed, then the problem is elsewhere in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.