0

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
3
Contributors
2
Replies
17
Views
3 Years
Discussion Span
Last Post by G_Waddell
0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.