Add rows in GridView dynamic with Textbox

ASP.Net GridView dynamic adds rows with Textbox:



In this post we describe how to add rows in asp.net grid view at run time. Here I give a example in which you adds row at run time and number of rows are also specific.

Related Post :


Create gridview row at run time in asp.net:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title> Creat row at run time</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <br />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="139px">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>city1</asp:ListItem>
            <asp:ListItem>city2</asp:ListItem>
            <asp:ListItem>city3</asp:ListItem>
            <asp:ListItem>city4</asp:ListItem>
            <asp:ListItem>city5</asp:ListItem>
        </asp:CheckBoxList>
        <br />
      
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        Number of Dynamic Created Rows =
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
    </div>
    <div align="center">
        <asp:GridView ID="Gridview1" runat="server"  AutoGenerateColumns="false"
            Visible="false" BackColor="#0066FF" BorderColor="Maroon" BorderStyle="Groove"
            BorderWidth="2px">
            <Columns>
                <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone No.">
                    <ItemTemplate>
                        <asp:TextBox ID="TxtPno" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gender">
                    <ItemTemplate>
                        <asp:TextBox ID="TxtGender" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <ItemTemplate>
                        <asp:TextBox ID="TxtAge" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Asan">
                    <ItemTemplate>
                        <asp:TextBox ID="TxtAsan" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="submit" runat="server" Text="submit" onclick="submit_Click" />
    </div>
    </form>
</body>
</html>

C# code for add gridview row at run time (dynamically):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
   
    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                   
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TxtName");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TxtPno");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TxtGender");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TxtAge");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TxtAsan");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                   
                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                   
                   TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TxtName");                      
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TxtPno");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TxtGender");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TxtAge");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TxtAsan");

                    string ct = string.Empty;
                    if (CheckBoxList1.Items[i].Selected==true)
                    {
                        box1.Text = dt.Rows[i]["Column1"].ToString();
                        ct = CheckBoxList1.Items[i].Text;                       
                        box1.Text = ct;
                    }
                    //box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                    box4.Text = dt.Rows[i]["Column4"].ToString();
                    box5.Text = dt.Rows[i]["Column5"].ToString();
                    rowIndex++;
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int count = 0;
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                count++;
            }

        }
        Label1.Text = count.ToString();
       
        for (int i = 1; i < count; i++)
        {
            Gridview1.Visible = true;
            AddNewRowToGrid();
        }
        uncheck();
    }

    public void uncheck()
    {
        foreach (ListItem li in CheckBoxList1.Items)
        {
            li.Selected = false;

        }
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow row in Gridview1.Rows)
            {
                TextBox txtName = (TextBox)row.FindControl("TxtName");
                Label1.Text = txtName.Text;

                //find all text boxes here......................
                //call saveinti_database function here.....
                Saveinto_Database();

            }
        }
        catch (Exception)
        {

        }
    }

    public void Saveinto_Database()
    {
        //code here for save values in data base.........
    }
}


Here we give the list of Examples related to gridview:


Comments