954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Clearing The Contents Of A Datagridview

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 :)

George_E
Newbie Poster
21 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

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.

If Grid2.SelectedCells.Count <> 0 Then
            Table2TableAdapter.Connection.Open()
            Dim I As Integer = Grid2.CurrentCell.RowIndex
            TestdbDataSet.Table2.Rows.Item(I).Delete()
            TestdbDataSet.GetChanges()
            Table2TableAdapter.Update(TestdbDataSet.Table2)
            Table2TableAdapter.Connection.Close()
        Else
            MsgBox("There are no items selected.")
        End If
cellus205
Junior Poster in Training
57 posts since May 2007
Reputation Points: 10
Solved Threads: 3
 

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

George_E
Newbie Poster
21 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

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:

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


And then you can delete data from the tempTable datatable instead of your actual database.

cellus205
Junior Poster in Training
57 posts since May 2007
Reputation Points: 10
Solved Threads: 3
 

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

George_E
Newbie Poster
21 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

So what you want is to Simply Clear the Grid ?

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

Dataset.Clear();
DataTable.Clear();


Simple ne

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

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


:(

George_E
Newbie Poster
21 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

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.

If strSearch = "" Then
            strSearch = txtItemId.Text
        Else
            strSearch = strSearch & " ' or [item_ID] = ' " & txtSearch.Text
            txtSearch.Clear()
        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

George_E
Newbie Poster
21 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

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!

Capritarius21
Newbie Poster
20 posts since Jan 2010
Reputation Points: 15
Solved Threads: 4
 

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

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

Thankyou for very helpful code.

vijay2040
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

Welcome vijay2040.

I'm glad you got it helpful. Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.

Thread Closed.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You