Wednesday, 23 April 2014

How to Clear the Gridview using asp.net?

Source code : 


 <asp:GridView ID="GridViewID" runat="server" 
     AllowSorting="true" AutoGenerateColumns="False"
     AllowPaging="true" CellPadding="4" DataKeyNames="DemoId"               EmptyDataText='No Records Found.'
      ForeColor="#333333" HeaderStyle-Font-Bold="true"  ShowFooter="False" Width="100%" PageSize="5">
   <EmptyDataRowStyle BackColor="White" BorderWidth="0px" />
   <PagerSettings Position="TopAndBottom" />
   <RowStyle BackColor="White" ForeColor="Black" />
   <PagerStyle BackColor="White" />
   <HeaderStyle Font-Bold="False" />
 </asp:GridView>


Server-side code:

Private Sub ClearGrid(ByVal gvw As GridView)
       gvw.DataSource = Nothing
            gvw.EditIndex = -1
            gvw.DataBind()
       
    End Sub
Call the method in Page Load:
If (Page.IsPostBack = False) Then
ClearGrid(GridViewID)
End If

Screen shot of clear Gridview when loading the page :




No comments:

Post a Comment