hi everyone
my question is how to clear a datagrid view in vb.net 2010
i have tried DatGridView1.Clear() but it only works once
and i would like to clear my datagrid every time i click the load button
hers my code

Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15.Click

        OpenFileDialog1.Title = "Please Select a File"
        OpenFileDialog1.InitialDirectory = "C:temp"
        OpenFileDialog1.ShowDialog()


    End Sub

Private Sub OpenFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        dgQuotation.DataSource = Nothing
        Dim strm As System.IO.Stream
        strm = OpenFileDialog1.OpenFile()
        strPath = OpenFileDialog1.FileName

        If Not (strm Is Nothing) Then
            dgQuotation.DataSource = Nothing
            ' Dim col As New DataGridViewTextBoxColumn
            'column declaration
            dgQuotation.ColumnCount = 43
            columnDeclaration()
            For Each line As String In System.IO.File.ReadAllLines(strPath)
                dgQuotation.Rows.Add(line.Split("!"))
            Next
            strm.Close()

        End If
    End Sub

Recommended Answers

All 2 Replies

Why not bind the datagridview to a bindingsource. the reset the bindingsource.
You will need a datatable or a collection but that should give you what you want. All you have to do is clear the datasource of the bindingsource and call the ResetBindings(False) method on the bindingsource

DataGridView1.Rows.Clear() will clear the rows for you.
If you want to completely clear everything from the grid you could then add DataGirdView1.Columns.Clear() to clear the columns as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.