Clear a datagridview

Xaphann

Member
Joined
Jun 17, 2011
Messages
5
Programming Experience
Beginner
Hi I am trying to clear a datagridview. Here is how the datagrid is populated;
VB.NET:
     con.Open()
        Dim myCommand As String = "select field1, field2 from table"
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(myCommand, con)
        da.Fill(ds, "table")
        con.Close()
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = ds.Tables("table")
Created a clear button with this code;
VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Clear()
    End Sub
Problem is that every time it returns a "System.ArgumentException was unhandled" error. So tried this;
VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        DataGridView1.DataSource = Nothing
    End Sub
This did not clear the datagrid at all. I am at a loss.

Thanks
 
The whole point of setting the DataSource is so that the grid displays whatever data is in that bound list. You can't then Clear the Rows of the grid because that would require the grid to be out of sync with the data source. That's why the first code snippet doesn't work. I can't see why the second one wouldn't work though. I just tried this:
Public Class Form1

    Private table As New DataTable

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With table.Columns
            .Add("ID", GetType(Integer))
            .Add("Name", GetType(String))
        End With

        With table.Rows
            .Add(1, "Peter")
            .Add(2, "Paul")
            .Add(3, "Mary")
        End With
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        DataGridView1.DataSource = table
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        DataGridView1.DataSource = Nothing
    End Sub

End Class
and it worked exactly as you'd expect.
 
Thank you for explaining why the first one did not work, makes perfect sense. unfortunately I made a mistake in posting this. In posting this I dumped down my program a bit too much. The program does not populate the datagridview on load. The user selects some filters and the hits a search button. The first time works fine, the second time the next search gets appended to last column. Like this;

VB.NET:
First search press;

                Field1          Field2
                Value1          Value2

Second search press

                Field1          Field2          Field1          Field2
                Value1          Value2          Value1          Value2


Here is a better example of my program;

VB.NET:
Private Sub searchButton_Click(sender As System.Object, e As System.EventArgs) Handles searchButton.Click


        DataGridView1 = Nothing


        Dim whereStatement As String
        'where statement logic not inclueded


        con.Open()
        Dim myCommand As String = "select field1, field2 from table where " & whereStatement
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(myCommand, con)
        da.Fill(ds, "table")
        con.Close()
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = ds.Tables("table")


        'Datagridview formating like rename columns and changing status fields to be user readable
        'Datagridview formating logic not included
    End Sub


thanks
 
Back
Top