Problem clearing datagridview

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
I hav etried out several solutions found on the net to solve my problem but I cant seem to find the right answer.

First my Code:

VB.NET:
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.IO


Public Class Form2

    Dim myConnection As SqlConnection
    Dim myCommand As SqlDataAdapter
    Dim myCommand2 As SqlDataAdapter
    Dim cbldr As SqlCommandBuilder
    Dim ds As DataSet
   



    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        

        Test()


    End Sub

    Sub Test()

        'Henter database tilkoplingen fra Form 1,
        myConnection = Form1.cnn

        myCommand = New SqlDataAdapter("SELECT * FROM Kundenavn", myConnection)

        ' Lag og fyll DataSet.
        ds = New DataSet()
        myCommand.Fill(ds, "Kundenavn")
        KundenavnDataGridView.DataSource = ds.Tables("Kundenavn")


        



    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.cnn.Open()

        cbldr = New SqlCommandBuilder(myCommand)
        myCommand.Update(ds, "Kundenavn")
        cbldr = New SqlCommandBuilder(myCommand)
        myCommand.Update(ds, "Ordre")

        Form1.cnn.Close()
        Test()
    End Sub

    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Henter ordre fra DB i he nhold til kundeID angitt i teksboks
        myCommand = New SqlDataAdapter("SELECT * FROM Ordre WHERE KundeID =" + TextBox1.Text + "", myConnection)
        myCommand.Fill(ds, "Ordre")
        OrdreDataGridView.DataSource = ds.Tables("Ordre")
    End Sub
End Class

I get my data into a dataGridview. but when i press button 2 again the grid does not clear it just adds the data beneath the first rows, how do I clear the grid before the next "fill" ???

Can anyone please tell me how to fix this??
 
Last edited:
ds.Clear() removes all rows in all tables, the same a dataTable.Clear for each table in the set, this is also equivalent of the the default TA.ClearBeforeFill option for strongly typed datasets.

I recommend you look into making the IDE write the Data Access code for you by using the designers:
Data Walkthroughs
Visual Basic How Do I Video Series

When you're writing commands with parameters manually I strongly recommend you parameterize them: SqlCommand.Parameters Property (System.Data.SqlClient)
 
my poin t is that I do not want to clear any tables, I need to clear the gridview.
Because; when I first call the fill command it fills the gridview withfor the moment 1 row because that is all there is in the ordre table. but if I call it one more time it adds a row in the gridview, but there is still only one row in the ordre table. that way when I call the update command everything halts because the grid shows 2 samples of the first row.
which is completely wrong. That is why I need to clear/empty the gridview before I call the fill command again. But I can not seem to find the right command to empty the grid..
 
If you think about it the grid provides only a view of the data, the data itself is in the DataSet/DataTable, so if you want to clear the current data you need to clear it from the datastore, which is what I explained in previous post ;)

And please do not mix the terms DataGridView and GridView, those are both common .Net controls, but for different environments, the grid control you're using is called DataGridView.
 
joh, you are always Fill()ing the dataset tables, and never clearing them. Like if you download a file and save it to your desktop
And then download it again and save it (renamed)..
..now you have 2 files
And then you download again and save it (renamed)..
..now you have 3 files

If you delete it first, then download it again, then you only have 1 file

So clear your table before you fill it :)
 
Ok I have tried to figure out how to do that, but I can not seem to find the right command,

Can yo please give me a hint?
 
Back
Top