views:

897

answers:

6

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?

A: 

I'd probably use this...

DataGridView1.Rows.Clear()

to clear out the rows and then rebind.

Jason Punyon
It says cannot clear this list..Is it because I'm using a database table as the datasource?
Bibhas
A: 

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

FiveTools
A: 

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
simon_bellis
A: 

I'm doing this -

Dim temp As DataTableCollection = Nothing
Dim DA As New SqlDataAdapter(Com.CommandText, con1)
DA.Fill(DS, "tblEmp")

DataGridView1.DataSource = temp
DataGridView1.Refresh()
DataGridView1.DataSource = DS.Tables("tblEmp")

Not working..

Bibhas
A: 

datagridview tutorial

http://vb.net-informations.com/datagridview/vb.net_datagridview_tutorial.htm

gever

gever
A: 

If the DataGridView is bound to any datasource, you'll have to set the DataGridView's DataSource property to Nothing.

If the DataGridView is not bound to any data source, this code will do the trick:

DataGridView.Rows.Clear()
Alex Essilfie