Click here to Skip to main content
15,031,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i add a new row to my grid view table when i click add new row button? it should display all my datum from the database table along with a new editable row in which i'm going to insert the values.....
Posted

YOu can use the following code to design the grid view in order to provide the user with insert facility and then you can use the gridview events to perform the required insert delete update operations:
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton ="true" AutoGenerateDeleteButton="true"
    AutoGenerateColumns ="false" onrowcommand="GridView1_RowCommand"
        onrowdeleted="GridView1_RowDeleted" onrowdeleting="GridView1_RowDeleting"
        onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated"
        onrowupdating="GridView1_RowUpdating" ShowFooter ="true" >
    <Columns>
    <asp:CommandField ButtonType ="Link" InsertText ="Insert" ShowInsertButton ="true" />
    <asp:TemplateField HeaderText ="CustomerID">
    <ItemTemplate>
        <asp:Label ID="lblcustomerid" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtcustomerid" runat="server"></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="ftcustomerid" runat="server"></asp:TextBox>
    </FooterTemplate>
     </asp:TemplateField>
    <asp:TemplateField HeaderText ="CustomerName">
    <ItemTemplate>
        <asp:Label ID="lblcustomername" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtcustomername" runat="server"></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="ftcustomername" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText ="Phoneno">
    <ItemTemplate>
        <asp:Label ID="lblphoneno" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtphoneno" runat="server"></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="ftphoneno" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText ="Address">
    <ItemTemplate>
        <asp:Label ID="lblAddress" runat="server" Text="Label"></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="ftAddress" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>


YOu can refer the following link to perform insert,delte,update in gridview
Insert, Update, Delete with Gridview....simple way
   
v2
Try this code..

C#
// Assuming yor are binding GridView using DataTable

 Protected void AddButton_Click(Object sender, EventArgs e)
 {
     DataTable Dt = (DataTable)(GridView1.DataSource);
     Dt.Rows.Add(Dt.NewRow());
     Dt.Rows[Dt.Rows.count-1]["Col1"]="Your Value";
     Dt.Rows[Dt.Rows.count-1]["Col2"]="Your Value";
     Dt.Rows[Dt.Rows.count-1]["Col3"]="Your Value";
     Dt.AcceptChanges();
     
     GridView1.EditIndex = Dt.Rows.count-1;
     GridView1.DataSource = Dt;
     GridView1.DataBind();
 }


Hope this helps..

Please mark this answer if your problem solved.

with regards
Karthik Harve

Happy Coding...
   
You can write a Javascript on the Add button.
onclick="addRow('your table name goes here')


and the javascript is

C#
function addRow(tableID)
    {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell1.appendChild(element1);
        var cell2 = row.insertCell(1);
        cell2.innerHTML = rowCount + 1;
        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell3.appendChild(element2);
    }
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900