943,614 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 9815
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 23rd, 2008
0

Clearing The Contents Of A Datagridview

Expand Post »
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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
George_E is offline Offline
21 posts
since Nov 2007
Oct 24th, 2008
0

Re: Clearing The Contents Of A Datagridview

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.

VB Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Oct 24th, 2008
0

Re: Clearing The Contents Of A Datagridview

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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
George_E is offline Offline
21 posts
since Nov 2007
Oct 24th, 2008
0

Re: Clearing The Contents Of A Datagridview

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:

VB Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Nov 14th, 2008
0

Re: Clearing The Contents Of A Datagridview

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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
George_E is offline Offline
21 posts
since Nov 2007
Nov 14th, 2008
0

Re: Clearing The Contents Of A Datagridview

So what you want is to Simply Clear the Grid ?

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

VB.NET Syntax (Toggle Plain Text)
  1. Dataset.Clear();


VB.NET Syntax (Toggle Plain Text)
  1. DataTable.Clear();

Simple ne
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007
Nov 20th, 2008
0

Re: Clearing The Contents Of A Datagridview

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


Reputation Points: 6
Solved Threads: 0
Newbie Poster
George_E is offline Offline
21 posts
since Nov 2007
Dec 18th, 2008
0

Re: Clearing The Contents Of A Datagridview

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.


VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
George_E is offline Offline
21 posts
since Nov 2007
Jan 14th, 2010
0

Clearing datagrid

I am developing an application right now and
having a hard time in clearing the
DataGrid contents.

I google the topic then
I found your answer.

Thanks, its a big help!

GOD BLESS!
Reputation Points: 15
Solved Threads: 3
Newbie Poster
Capritarius21 is offline Offline
20 posts
since Jan 2010
Jan 15th, 2010
0
Re: Clearing The Contents Of A Datagridview
I am developing an application right now and
having a hard time in clearing the
DataGrid contents.

I google the topic then
I found your answer.

Thanks, its a big help!

GOD BLESS!
let me see the code that you bind your Grid with
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: Visual Basic 2008 to excel 2007
Next Thread in VB.NET Forum Timeline: comment Multiple lines





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC