Clearing The Contents Of A Datagridview

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 21
Reputation: George_E is an unknown quantity at this point 
Solved Threads: 0
George_E George_E is offline Offline
Newbie Poster

Clearing The Contents Of A Datagridview

 
0
  #1
Oct 23rd, 2008
Hey guys

As many of you are well aware I creating an EPOS System

I have a datagrid that stores details about all of the items which the user wishes to buy in the transaction.

However if the user changes his mind I have a feature that cancels the row in the datagrid.

This is my code for this feature:

        With Me.DataGridView1

            For Each r As DataGridViewRow In .SelectedRows


                .Rows.RemoveAt(r.Index)
                DataGridView1.Update()

            Next

        End With

The code functions fine, however a problem arises if I enter a new row into the datagridview, the previous row I recently cancelled appears again.

Does anyone know how I solve this problem??

All ideas welcome
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #2
Oct 24th, 2008
You have to remove the item from the database that the datagrid is getting the information from, instead of just the actual datagrid. Heres a example.

  1. If Grid2.SelectedCells.Count <> 0 Then
  2. Table2TableAdapter.Connection.Open()
  3. Dim I As Integer = Grid2.CurrentCell.RowIndex
  4. TestdbDataSet.Table2.Rows.Item(I).Delete()
  5. TestdbDataSet.GetChanges()
  6. Table2TableAdapter.Update(TestdbDataSet.Table2)
  7. Table2TableAdapter.Connection.Close()
  8. Else
  9. MsgBox("There are no items selected.")
  10. End If
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: George_E is an unknown quantity at this point 
Solved Threads: 0
George_E George_E is offline Offline
Newbie Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #3
Oct 24th, 2008
Thanks for your response

I have taken your idea into account but I would end up deleting the items from the database that way.

Isnt there a way of clearing the datagrid without deleting the items??

Looking forward to your replies


George
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #4
Oct 24th, 2008
Well. Im still kinda new to VB .NET, but Datagrids are basically only representative of a dataset, they dont actually store the data. So in effect, the datagrid only shows what the actual dataset contains. One way you can get around deleting the data from the actual dataset is to create a copy of the dataset, and actually delete data from that set. Heres an example:

  1. Dim tempTable As New testdbDataSet.Table1
  2. Table1TableAdapter.Fill(tempTable)
  3. DataGrid.DataSource = tempTable

And then you can delete data from the tempTable datatable instead of your actual database.
Last edited by cellus205; Oct 24th, 2008 at 1:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: George_E is an unknown quantity at this point 
Solved Threads: 0
George_E George_E is offline Offline
Newbie Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #5
Nov 14th, 2008
Hey guys

thankyou for your responses,

I did leave this problem and continued to develop the rest of the system however it appears it is affecting the rest of system. Whenever I empty textboxes and attempt to populate them again the previous result appears in the textbox instead of the new one.

I do believe that Cellus response is correct but what troubles me now is that how to empty the dataset?

I have searched thoroughly on the internet and cant find an answer

Any ideas??

Your responses are much appreciated

George
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 155
Reputation: vuyiswamb is an unknown quantity at this point 
Solved Threads: 5
vuyiswamb's Avatar
vuyiswamb vuyiswamb is offline Offline
Junior Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #6
Nov 14th, 2008
So what you want is to Simply Clear the Grid ?

If so if you are using a Dataset , you just have to say

  1. Dataset.Clear();


  1. DataTable.Clear();

Simple ne
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: George_E is an unknown quantity at this point 
Solved Threads: 0
George_E George_E is offline Offline
Newbie Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #7
Nov 20th, 2008
Hey guys,

thanks for your responses but it doesnt seem to be solving my problem. Instead of the datagrid / textbox displaying the results of a new query it keeps retrieving the results from the previous query


Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: George_E is an unknown quantity at this point 
Solved Threads: 0
George_E George_E is offline Offline
Newbie Poster

Re: Clearing The Contents Of A Datagridview

 
0
  #8
Dec 18th, 2008
Hey guys, I have found the root to the problem.

Below is a snippet of code I used in order to retrieve several query results items into the datagrid.


  1.  
  2. If strSearch = "" Then
  3. strSearch = txtItemId.Text
  4. Else
  5. strSearch = strSearch & " ' or [item_ID] = ' " & txtSearch.Text
  6. txtSearch.Clear()
  7. End If

The above snippet of code stores my queries into string.

What we were doing previously to clear the data of the datagrid was correct, however the above snippet of code which would store my query result as a string, would then load up the same string again whenever I would click the search buttion.

By removing this snippet of code, it would solve the problem, however, I wouldnt be able to hold more than one query result in the datagrid at a time then.

Does anybody know how I could solve this, or tackle it??

Any feedback is appreciated

George
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC