VB.NET: Clear DataGridView

Viewed 309107

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?

28 Answers

I'd probably use this...

DataGridView1.Rows.Clear()

to clear out the rows and then rebind.

Can you not bind the datagridview to an empty collection (instead of null). That do the trick?

I've got this code working in a windows form,

Public Class Form1

    Private dataStuff As List(Of String)


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

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dataStuff = New List(Of String)

        dataStuff.Add("qwerty")
        dataStuff.Add("another")
        dataStuff.Add("...and another")

        DataGridView1.DataSource = dataStuff
    End Sub
End Class

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.

Use this code where ever you want to implement clear datagridview command

datagridview1.datasource= nothing
datagridview1.datasource= ds
dt.clear()               'Dt as new DATATABLE
ds.clear()              'Ds as new Dataset

this code will clear the datagridview and wil stop data duplication when populating data from database.

Try this for a specific cell:

Datagrid.Rows(RowIndex).Cells(ColIndex).Value = DBNull.Value

DgRemolques is the name of my Data grid on WPF

dgRemolques.ItemsSource = Nothing but i need to clean also de DataTable dtRemolques.Clear()

Related