You must Sign In to post a response.
  • Category: ASP.NET

    After click the button i need to clear the gridview data using asp.net

    i need to clear the gridview data after click the button .
  • #762275
    Hi

    After click if you need clear the gridview means try this code

    Server Side Bind Records


    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {

    DataTable dt = new DataTable();
    dt.Columns.Add("EMPID");
    dt.Columns.Add("EMPNAME");
    DataRow dr;
    dr = dt.NewRow();
    dr[0] = 1001;
    dr[1] = "Jayakumar";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 1001;
    dr[1] = "Jayakumar";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 1001;
    dr[1] = "Jayakumar";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr[0] = 1001;
    dr[1] = "Jayakumar";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr[0] = 1001;
    dr[1] = "Jayakumar";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr[0] = 1001;
    dr[1] = "Jayakumar";
    dt.Rows.Add(dr);

    GrdData1.DataSource = dt;
    GrdData1.DataBind();
    GrdData1.HeaderRow.Cells[0].Text = "Employee ID";
    GrdData1.HeaderRow.Cells[1].Text = "Employee Name";
    }
    }



    Client Side



    <div id="div1" style="overflow:auto; width:400px;height:150px;">
    <asp:GridView ID="GrdData1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField = "EMPID" HeaderText = "EMPID ID" />
    <asp:BoundField DataField = "EMPNAME" HeaderText = "EMPNAME" />

    <asp:TemplateField ItemStyle-Width = "30px" HeaderText = "EMPID">
    <ItemTemplate>
    <asp:LinkButton ID="lnkEditRecord" runat="server" Text = "Edit" OnClick="lnkEditRecord_Click"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>

    <asp:Button ID="BtClear" runat="server" Text="Clear" OnClick="BtClear_Click" />


    Clear Gridview Event


    protected void BtClear_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    GrdData1.DataSource = dt;
    GrdData1.DataBind();
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762286
    Hi,

    You can assign null to datasource, this will help us to clear the gridview data.


    protected void btnClear_Click(object sender, EventArgs e)
    {
    gv.DataSource=null;
    gv.DataBind();
    }


    Try something like above to achieve your goal.

    Hope this will helpful to you..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762287
    Hello Selvi,

    You can do it by using the below mentioned code :

    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>Pen</asp:ListItem>
    <asp:ListItem>Pencil</asp:ListItem>
    <asp:ListItem>Book</asp:ListItem>
    <asp:ListItem>Eraser</asp:ListItem>
    </asp:DropDownList>
    <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
    <asp:Button ID="ButtonAdd" runat="server" onclick="ButtonAdd_Click"
    Text="Add" />
    <asp:Button ID="ButtonClearGrid" runat="server" onclick="ButtonClearGrid_Click"
    Text="Clear Grid" />
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" CellPadding="4"
    Font-Names="Segoe UI" ForeColor="#333333"
    EmptyDataText="No Record(s) found">
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>


    protected void ButtonClearGrid_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    GridView1.DataSource = dt;
    //GridView1.DataSource = null; // You can use this also...
    GridView1.DataBind();
    }

    See the attached image as an output of this code.

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762308
    Hi

    To clear gridview you can first clear the data table, then assign the gridview1.datasource=datatable;

    Or esle you can assign directly null for gridview.

    datatable.Clear();
    GridView1.DataSource = datatable;
    GridView1.DataBind();

    or

    GridView1.DataSource = null;
    GridView1.DataBind();


    Regards

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.


Sign In to post your comments