VB.NET: Clear DataGridView

2019-02-01 21:02发布

I've tried -

DataGridView1.DataSource=Nothing

and

DataGridView1.DataSource=Nothing
DataGridView1.Refresh()

and

DataGridView1.RefreshEdit()

None of them works..

I've written a method that sets the DataSource of the DataGridView when executed. but each time i execute it, it replicates the data with new value and appends it to the previous contents of the DGV.. I wanna clear the content and then add the values.. Is that possible?

25条回答
一夜七次
2楼-- · 2019-02-01 21:50

I had the same problem: I was programmatically binding my GridView1 to one SQL table [dictonary] or another [meny] BUT when I selected the second table from my RadioButtonList1, I was getting an error (System.Web.HttpException: Field or property with the title [the first column's title from the previously selected table] was not found in the selected data source.) i.e. saying that the columns from my first-selected table couldn't be found. All I needed to do was insert:

GridView1.Columns.Clear()

before adding the table columns. Here goes the full code:

Dim connectionString As String = "your-string-details"
Dim connection As New SqlConnection(connectionString)

Then comes your first Sub:

Private Sub BindOrders()
    connection.Open()

    Dim sqlCommand As String = "SELECT * FROM [dictionary]" 
    Dim dataAdapter As New SqlDataAdapter(sqlCommand, connection)
    Dim dt As New DataTable() 
    dataAdapter.Fill(dt)

    GridView1.Columns.Clear() ' clear columns before adding new ones

    If GridView1.Columns.Count <= 0 Then
        Dim Field As New BoundField()
        Field.DataField = "id"
        Field.HeaderText = "id"
        GridView1.Columns.Add(Field)

        Field = New BoundField()
        Field.DataField = "strArHundreds"
        Field.HeaderText = "strArHundreds"
        GridView1.Columns.Add(Field)

        Field = New BoundField()
        Field.DataField = "strArTens"
        Field.HeaderText = "strArTens"
        GridView1.Columns.Add(Field)

        Field = New BoundField()
        Field.DataField = "strArSingles"
        Field.HeaderText = "strArSingles"
        GridView1.Columns.Add(Field)
    End If

    GridView1.DataSource = dt
    GridView1.DataBind()

    connection.Close()
End Sub

Then comes your second Sub:

Private Sub BindDocuments()
    connection.Open()

    Dim sqlCommand As String = "SELECT * FROM [meny]"
    Dim dataAdapter As New SqlDataAdapter(sqlCommand, connection)
    Dim dt As New DataTable()

    dataAdapter.Fill(dt)

    GridView1.Columns.Clear() ' clear columns before adding new ones

    If GridView1.Columns.Count <= 0 Then
        Dim Field As New BoundField
        Field = New BoundField
        Field.DataField = "id"
        Field.HeaderText = "id"
        GridView1.Columns.Add(Field)

        Field = New BoundField
        Field.DataField = "nazev"
        Field.HeaderText = "nazev"
        GridView1.Columns.Add(Field)
    End If

    GridView1.DataSource = dt
    GridView1.DataBind()

    connection.Close()
End Sub

Finally comes your RadioButton:

Protected Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged
    Dim index As Integer
    index = RadioButtonList1.SelectedIndex
    Select Case index
        Case 0
            BindOrders()
            Exit Select
        Case 1
            BindDocuments()
            Exit Select
    End Select
End Sub

For completion, here is the code for the GridView1 and the RadioButtonList1 in the associated aspx.file:

<asp:RadioButtonList ID="RadioButtonList1"
    runat="server"
    AutoPostBack="True"
    OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
    <asp:ListItem>Obraty</asp:ListItem>
    <asp:ListItem>Dokumenty</asp:ListItem>
</asp:RadioButtonList>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
</asp:GridView>

This all works well now.

查看更多
混吃等死
3楼-- · 2019-02-01 21:51

Don't do anything on DataGridView, just clear the data source. I tried clearing myDataset.clear() method, then it worked.

查看更多
放荡不羁爱自由
4楼-- · 2019-02-01 21:51

1) create button named it Clear.Inside insert tfhe following code datagridviewer.DataSource=nothing

2) In your search button begin your code by the following statement

datagridviewer.DataSource = DataSet.table

Nb:instead of table put the real name of your table ex: datagridviewer.DataSource = DataSet.client

查看更多
唯我独甜
5楼-- · 2019-02-01 21:51

You can also try this code if you want to clear all data in your DataGridView

DataGridView1.DataSource.Clear()
查看更多
做个烂人
6楼-- · 2019-02-01 21:52

You can do in this way:

DataGridView1.Enable = false
DataGridView1.DataSource = Nothing
DataGridView1.Enable = true
查看更多
smile是对你的礼貌
7楼-- · 2019-02-01 21:53

The mistake that you are making is that you seem to be using a dataset object for storing your data. Each time you use the following code to put data into your dataset you add data to the data already in your dataset.

myDataAdapter.Fill(myDataSet)

If you assign the table in your dataset to a DataGridView object in your program by the following code you will get duplicate results because you have not cleared data which is already residing in your dataset and in your dataset table.

myDataGridView.DataSource = myDataSet.Tables(0)

To avoid replicating the data you have to call clear method on your dataset object.

myDataSet.clear()

An then assign the table in your dataset to your DataGridView object. The code is like this.

myDataSet.clear()
myDataAdapter.Fill(myDataSet)
myDataGridView.DataSource = myDataSet.Tables(0)

You can try this code:

' clear previous data
DataGridView2.DataSource = Nothing
DataGridView2.DataMember = Nothing
DataGridView2.Refresh()
Try
    connection.Open()
    adapter1 = New SqlDataAdapter(sql, connection)
    ' clear data already in the dataset
    ds1.Clear()
    adapter1.Fill(ds1)
    DataGridView2.DataSource = ds1.Tables(0)
    connection.Close()
Catch ex As Exception
    MsgBox(ex.ToString)
End Try
查看更多
登录 后发表回答