Adding Dynamic Rows in ASP.Net GridView Control with Textbox
Dynamically Rows in ASP.Net GridView Control with Textbox:
The core idea here is that from the code-behind if we
dynamically change the DataSource of GridView And Add Row At runtime.
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default16.aspx.cs"
Inherits="Default16"
%>
<!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 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>
Create ASP.NET GridView with dynamic Text Field by C# code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default16 :
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");
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();
uncheck();
for (int i = 1; i
< count; i++)
{
Gridview1.Visible = true;
AddNewRowToGrid();
}
}
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.........
}
}
Add rows dynamically to gridview:
ASP.Net GridView Control with Textbox |
Checkboxlist related other post on asp.net tutorial.
- Validation checkbox control using JavaScript
- Asp-checkboxlist control demo:
- Asp checkboxlist Example using C#
- Checkboxlist in asp.net(control example)
- Example jQuery Validate on CheckBoxList using C#
- Check Uncheck all html CheckBoxlist controls using jQuery:
- Check Uncheck all asp.net CheckBox in asp.net using jQuery
- Ckeck box list example using javascript.
- Asp.net checkboxlist control example
- How to use CheckBox control in asp.net
- How to check If checkbox Is checked in asp.net
- Validation checkbox control using JavaScript:
- Checkbox list Example using javascript
- Checkboxlist control in asp net
- Asp.net Checkbox List.
- Asp.net CheckBoxList using jQuery
- Get Asp.net CheckBoxList control values using Jqury
- Limit Number of Characters in a TextArea using jQuery
- Limitation of Characters in Textbox or TextArea in asp.netusing jquery:
Comments
Post a Comment