How do I clear a gridview?

Gridview retains previous search results and appends record

Page 1 of 1

1 Replies - 10236 Views - Last Post: 20 October 2010 - 04:08 PM Rate Topic: -----

#1 georgej28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 13-July 10

How do I clear a gridview?

Posted 20 October 2010 - 12:52 PM

I am using vs2005 asp.net( vb ). I have a page with a textbox, button and grid. The problem is clearing the gridview before each search. It keeps appending the new search results to the exiting grid view. I want to completly empty the grid/page and populate the gridview with just the new search records.

Ive tried
 GridView2.Columns.Clear()
        GridView2.Dispose()
        GridView2.DataSource = ""
        GridView2.DataBind()


with no luck

Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   

Dim myConnStr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

        Dim strSqlCount As String

        On Error GoTo AdoError

        GridView2.Columns.Clear()
        GridView2.Dispose()
        GridView2.DataSource = ""
        GridView2.DataBind()

strSqlCount= "SELECT * FROM tblInvoice WHERE InvoiceNumber =" & "'" & strBoxInvoiceNum01 & "'"
                
        AcctConnect = New OleDb.OleDbConnection(myConnStr)
        AcctConnect.Open()

        RsAcctCountAdapter = New OleDb.OleDbDataAdapter(strSqlCount, AcctConnect)
        RsAcctCountAdapter.Fill(RsAcctCount, "Count")
        
        GridView2.DataSource = RsAcctCount.Tables("Count").DefaultView
        GridView2.DataBind()

        RsAcctCountAdapter.Dispose()
        RsAcctCount.Dispose()
        AcctConnect.Close()

   
Done:

        Exit Sub


AdoError:
        If Str(Err.Number) <> 0 Then
            strTmp = strTmp & vbCrLf & "VB Error # " & Str(Err.Number)
            strTmp = strTmp & vbCrLf & "   Generated by " & Err.Source
            strTmp = strTmp & vbCrLf & "   Description  " & Err.Description

            MsgBox(strTmp)

            On Error GoTo 0
            GoTo Done
        End If


Is This A Good Question/Topic? 0
  • +

Replies To: How do I clear a gridview?

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How do I clear a gridview?

Posted 20 October 2010 - 04:08 PM

Since you are databinding to the GridView you don't need to do anything. Once you set the new DataSource and call DataBind, then the only data that will be displayed is what exists in the new DataSource.

Can you post the source and all the code-behind for the page showing the GridView?

Does strBoxInvoiceNum01 contain a value when you create the Select statement?

You don't appear to set the value of strBoxInvoiceNum01 in the button click event, so where does it get set?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1