Click here to Skip to main content
12,944,138 members (53,101 online)
Click here to Skip to main content


137 bookmarked
Posted 8 Jul 2016

Dynamically adding and deleting rows from ASP.NET GridView

, 8 Jul 2016 CPOL
This article talks about a small project that presents the idea of having a gridview which facilitate the addition and removal of rows dynamically at run time.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Add Dynamic Rows In Gridview</title>
    <form id="form1" runat="server">
            <asp:GridView ID="grvStudentDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"
                CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
                Width="97%" Style="text-align: left">
                    <asp:BoundField DataField="RowNumber" HeaderText="SNo" />
                    <asp:TemplateField HeaderText="Student Name">
                            <asp:TextBox ID="txtName" runat="server" MaxLength="50"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                    <asp:TemplateField HeaderText="Student Age">
                            <asp:TextBox ID="txtAge" runat="server" MaxLength="3" Width="66px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAge"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                    <asp:TemplateField HeaderText="Student Address">
                            <asp:TextBox ID="txtAddress" runat="server" Height="55px" TextMode="MultiLine"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                    <asp:TemplateField HeaderText="Gender">
                            <asp:RadioButtonList ID="RBLGender" runat="server" RepeatDirection="Horizontal">
                                <asp:ListItem Value="M">Male</asp:ListItem>
                                <asp:ListItem Value="F">Female</asp:ListItem>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="RBLGender"
                    <asp:TemplateField HeaderText="Qualification">
                            <asp:DropDownList ID="drpQualification" runat="server">
                                <asp:ListItem Value="G">Graduate</asp:ListItem>
                                <asp:ListItem Value="P">Post Graduate</asp:ListItem>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="drpQualification"
                                ErrorMessage="*" InitialValue="Select"></asp:RequiredFieldValidator>
                        <FooterStyle HorizontalAlign="Right" />
                            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
                    <asp:CommandField ShowDeleteButton="True" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            <br />
                <asp:Button ID="btnSave" runat="server" Text="Save Data" OnClick="btnSave_Click" />

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
I am a Software Engineer from Bhopal. I started my Career from Programming in ASP and now working as a Web Developer in ASP.Net (C#). I Love coding and always ready to gain new thing and always been towards Microsoft Technologies. Apart from coding my other hobbies are traveling, Internet Surfing, spending time with family and hang out with friends.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 9 Jul 2016
Article Copyright 2016 by AshishChaudha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid