how to clear the data in the GridView

Posted on 2009-04-28
Last Modified: 2012-05-06

I have a page where i am using VB as the language

I am using gridview to populate data from the database.  I have a select botton in each row and on selectedindexchanged i am populating another gridview.

OnRowDatabound on gridview1 i am assigning the values of each row to the respective select button.

Now how do i clear the data from the gridview1.
NOTE: In gridview1 only the select button is visible which has the row values.
Question by:Shiva_Kumar
    LVL 14

    Accepted Solution


    Just set the DataSource of GridView to null;


    It will clear all the rows. Since it has no databindings the GridView1 will disappear from the page. If you want to retain the header of the gridview you have to clear the rows from the datasource and bind with the GridView

    Author Comment


    I have tried your suggestion it, didn't work.

    what i notice is, it goes off as you mentioned for a flash and it comes back with all the data immediately.

    any other suggestion?

    LVL 14

    Expert Comment

    If it comes back, then you are binding data to the GridView again. Can you post the code?

    Author Comment

    i have a ajax tab control and under each tabpanel i have 3 gridview.

    Onload i am populating the data on the first gridview of all the tabpanel.

    On datarowbound of all the grid view i am using the following code to replace the select button with the row data

    Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ISSAVQuestionCategory.RowDataBound
            Dim row As GridViewRow = e.Row

            Dim selectCell As TableCell = row.Cells(0)
            If (selectCell.Controls.Count > 0) Then

                Dim selectControl As LinkButton = selectCell.Controls(0)
                If Not IsDBNull(selectControl) Then

                    selectControl.Text = e.Row.Cells(1).Text
                    e.Row.Cells(1).Visible = False

                End If
            End If
        End Sub

    now when somebody clicks on the select button (the row data) it popluates the second gridview of that particular tabpanel.

    now what i want is when somebody changes the tab it should remove the data from the second gridview of all the tabpanels, so i am using the the activetabchanged function of the tabcontainer to call a javascript which intrurn calls the following function to remove the second gridview of the tabpanel

    Public Sub noteproduct(ByVal tabindex As Integer)
            If tabindex = 0 Then
                Product = "abc"
            ElseIf tabindex = 1 Then
                Product = "xyz"
    me.Gridview2.datasource = nothing
     End Sub


    i am binding the data to gridview2 only once that is when somebody clicks on gridview1 - selectedindexchanged function

    Hope this helps,


    Author Comment


    You were right.  It was binding the data again.  

    Thanks for you help.. the problem is fixed now.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now