Tuesday, September 15, 2015

Asp.net add multiple rows to Gridview dynamically

In this article I am going to explain how to add multiple rows to Gridview control dynamically in asp.net

Description:

In this article I am going to add the multiple rows to Gridview on button click.

Implementation:
Method 1:
In this method when user clicks on button it continually added the row to Gridview control.


HTML Markup:
<asp:GridView ID="gvbook" runat="server" AutoGenerateColumns="false">
    <Columns>
         <asp:TemplateField HeaderText="SL No.">
                <ItemTemplate>
                    <%#Container.DataItemIndex +1 %>
                </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Book Name">
            <ItemTemplate>
                <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("name") %>'>
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Author">
            <ItemTemplate>
                <asp:TextBox ID="txtauthor" runat="server" Text='<%# Eval("author") %>'>
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:TextBox ID="txtprice" runat="server" Text='<%# Eval("price") %>'>
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="btnAddNewRow" runat="server" Text="Add New Row"/>

On button click write the below given code:
C#:
protected void btnAddNewRow_Click(object sender, EventArgs e)
    {
        try
        {
            var rows = gvbook.Rows.Cast<GridViewRow>().Select(a => new
            {
                Name = ((TextBox)a.FindControl("txtName")).Text,
                author = ((TextBox)a.FindControl("txtauthor")).Text,
                price = ((TextBox)a.FindControl("txtprice")).Text,
            }).ToList();
           
        rows.Add(new
            {
                Name = "",
                author = "",
                price=""
            });
            gvbook.DataSource = rows;
            gvbook.DataBind();          
        }
        catch(Exception ex)
        { }
    }

VB:
  Protected Sub btnAddNewRow_Click(sender As Object, e As EventArgs) Handles btnAddNewRow.Click
        Try
            Dim rows = gvbook.Rows.Cast(Of GridViewRow)().[Select](Function(a) New With {
                Key .Name = DirectCast(a.FindControl("txtName"), TextBox).Text,
                Key .author = DirectCast(a.FindControl("txtauthor"), TextBox).Text,
                Key .price = DirectCast(a.FindControl("txtprice"), TextBox).Text
            }).ToList()
            rows.Add(New With {
                Key .Name = "",
                Key .author = "",
                Key .price = ""
            })
            gvbook.DataSource = rows
            gvbook.DataBind()
        Catch ex As Exception
        End Try
    End Sub


Method 2:
In this method user enter the number of record he wants to add to Gridview and click to button. e.g. if want to add 3 rows user enter 3 in textbox and hit the button, it added the 3 rows to Gridview control.

HTML Markup:
<asp:TextBox ID="txtrow" runat="server"></asp:TextBox> &nbsp; &nbsp; &nbsp;<asp:Button ID="btnAddNewRow" runat="server" Text="Add New Row" OnClick="btnAddNewRow_Click" />
        <asp:GridView ID="gvbook" runat="server" AutoGenerateColumns="false" CellPadding="5">
    <Columns>
         <asp:TemplateField HeaderText="SL No.">
                <ItemTemplate>
                    <%#Container.DataItemIndex +1 %>
                </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Book Name">
            <ItemTemplate>
                <asp:TextBox ID="txtname" runat="server">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Author">
            <ItemTemplate>
                <asp:TextBox ID="txtauthor" runat="server">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:TextBox ID="txtprice" runat="server">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

On button click write the below given code:
C#:
protected void btnAddNewRow_Click(object sender, EventArgs e)
    {
        try
        {
            List<int> noofRows = new List<int>();
            int rows = 0;
            int.TryParse(txtrow.Text.Trim(), out rows);
            for (int i = 0; i < rows; i++)
            {
                noofRows.Add(i);
            }
            gvbook.DataSource = noofRows;
            gvbook.DataBind();
        }
        catch(Exception ex)
        { }
    }

VB:
Protected Sub btnAddNewRow_Click(sender As Object, e As EventArgs) Handles btnAddNewRow.Click
       Try
            Dim noofRows As New List(Of Integer)()
            Dim rows As Integer = 0
            Integer.TryParse(txtrow.Text.Trim(), rows)
            For i As Integer = 0 To rows - 1
                noofRows.Add(i)
            Next
            gvbook.DataSource = noofRows
            gvbook.DataBind()
        Catch ex As Exception
        End Try
    End Sub

Build and run the project/website. Check the result.

  In this article we have learn to how to add multiple rows to Gridview dynamically  in asp.net using C# and VB.netI hope you enjoyed this article. 

No comments:

Post a Comment