Adding rows dynamically to grid views by two ways..complete code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddingRow.aspx.cs" Inherits="AddingRow_HiddenField_AddingRow" %>
<!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>Untitled Page</title>
<script language="javascript">
function FillHiddenFields(caseid,cntrlid)
{
switch(caseid)
{
case 1:
document.getElementById("hdnEmpId").value = document.getElementById(cntrlid).value;
break;
case 2:
document.getElementById("hdnEmpName").value = document.getElementById(cntrlid).value;
break;
case 3:
document.getElementById("hdnSalary").value = document.getElementById(cntrlid).value;
break;
case 4:
document.getElementById("hdnDepartment").value = document.getElementById(cntrlid).value;
}
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<th>
<b>Employee Details</b>
</th>
</tr>
<tr>
<td>
<asp:Button ID="btnAddRowToGridView" runat="server" Text="AddRowToGridView"
onclick="btnAddRowToGridView_Click" />
<asp:Button ID="btnSetValue" runat="server" Text="btnSetValue" onclick="btnSetValue_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="dgEmployeeDetails" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" PageSize="2" AutoGenerateSelectButton="true"
OnSelectedIndexChanged="dgEmployeeDetails_SelectedIndexChanged" >
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
<td>
<table align="center" style="border-style:solid">
<tr>
<th colspan="2">
<b>Selected value Display</b>
</th>
</tr>
<tr>
<td>
Emp ID
</td>
<td>
<asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Emp Name
</td>
<td>
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Department
</td>
<td>
<asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<table align="left" style="border-style:solid">
<tr>
<th colspan="2">
<b>Add Row To Grid View</b>
</th>
</tr>
<tr>
<td>
Emp ID
</td>
<td>
<asp:TextBox ID="txtAddEmpiD" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Emp Name
</td>
<td>
<asp:TextBox ID="txtAddEmpname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Salary
</td>
<td>
<asp:TextBox ID="txtAddSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Department
</td>
<td>
<asp:TextBox ID="txtAddDepartment" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnAdd" runat="server" Text="SubmitRow"
onclick="btnAdd_Click" /><input type="hidden" id="hdnEmpId" runat="server" />
<input type="hidden" id="hdnEmpName" runat="server" />
<input type="hidden" id="hdnSalary" runat="server" />
<input type="hidden" id="hdnDepartment" runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.cs file
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 AddingRow_HiddenField_AddingRow : System.Web.UI.Page
{
//ViewState["order"]="asc";
protected void Page_Load(object sender, EventArgs e)
{
btnSetValue.Visible = false;
//dgEmployeeDetails.DataSource = Session["TabletoBind"];
//dgEmployeeDetails.DataBind();
if (!IsPostBack)
{
Session["TabletoBind"] = GetDataSource();
dgEmployeeDetails.DataSource = Session["TabletoBind"];
dgEmployeeDetails.DataBind();
//dgEmployeeDetails.AllowSorting = true;
//dgEmployeeDetails.PageSize = 2;
ShowFirstRow();
}
}
protected void dgEmployeeDetails_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow selectedRow = dgEmployeeDetails.SelectedRow;
txtEmpID.Text = selectedRow.Cells[1].Text;
txtEmpName.Text = selectedRow.Cells[2].Text;
txtSalary.Text = selectedRow.Cells[3].Text;
txtDepartment.Text = selectedRow.Cells[4].Text;
}
//protected void dgEmployeeDetails_PageIndexChanged(object sender, EventArgs e)
//{
// ShowFirstRow();
//}
//protected void dgEmployeeDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
//{
// dgEmployeeDetails.PageIndex = e.NewPageIndex;
// dgEmployeeDetails.DataSource = Session["TabletoBind"];
// dgEmployeeDetails.DataBind();
//}
//protected void dgEmployeeDetails_Sorting(object sender, GridViewSortEventArgs e)
//{
// DataTable dt = Session["TabletoBind"] as DataTable;
// if (dt != null)
// {
// //Sort the data.
// dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
// dgEmployeeDetails.DataSource = Session["TabletoBind"];
// dgEmployeeDetails.DataBind();
// }
//}
//private string GetSortDirection(string column)
//{
// // By default, set the sort direction to ascending.
// string sortDirection = "ASC";
// // Retrieve the last column that was sorted.
// string sortExpression = ViewState["SortExpression"] as string;
// if (sortExpression != null)
// {
// // Check if the same column is being sorted.
// // Otherwise, the default value can be returned.
// if (sortExpression == column)
// {
// string lastDirection = ViewState["SortDirection"] as string;
// if ((lastDirection != null) && (lastDirection == "ASC"))
// {
// sortDirection = "DESC";
// }
// }
// }
// ViewState["SortDirection"] = sortDirection;
// ViewState["SortExpression"] = column;
// return sortDirection;
//}
protected DataTable GetDataSource()
{
DataTable tableToBind = new DataTable("EmployeesDetails");
DataColumn column;
DataRow row;
//first Column
column = new DataColumn("EmpID", System.Type.GetType("System.String"));
//column.ReadOnly = true;
//column.Unique = true;
tableToBind.Columns.Add(column);
//second column
column = new DataColumn("Emp Name", System.Type.GetType("System.String"));
//column.ReadOnly = true;
tableToBind.Columns.Add(column);
//third column
column = new DataColumn("Salary", System.Type.GetType("System.String"));
// column.ReadOnly = true;
tableToBind.Columns.Add(column);
column = new DataColumn("Department", System.Type.GetType("System.String"));
//column.ReadOnly = true;
tableToBind.Columns.Add(column);
//Add rows to table
row = tableToBind.NewRow();
row["EmpID"] = 1;
row["Emp Name"] = "Mark";
row["Salary"] = 10000;
row["Department"] = "Finance";
tableToBind.Rows.Add(row);
//2nd row
row = tableToBind.NewRow();
row["EmpID"] = 2;
row["Emp Name"] = "Jack";
row["Salary"] = 30000;
row["Department"] = "Marketing";
tableToBind.Rows.Add(row);
// 3 row
row = tableToBind.NewRow();
row["EmpID"] = 3;
row["Emp Name"] = "Hussy";
row["Salary"] = 20000;
row["Department"] = "System";
tableToBind.Rows.Add(row);
//4th row
row = tableToBind.NewRow();
row["EmpID"] = 4;
row["Emp Name"] = "Robin";
row["Salary"] = 40000;
row["Department"] = "System";
tableToBind.Rows.Add(row);
return tableToBind;
}
//protected DataTable CreateTable()
//{
//}
protected void ShowFirstRow()
{
GridViewRow selectedRow = dgEmployeeDetails.Rows[0];
txtEmpID.Text = selectedRow.Cells[1].Text;
txtEmpName.Text = selectedRow.Cells[2].Text;
txtSalary.Text = selectedRow.Cells[3].Text;
txtDepartment.Text = selectedRow.Cells[4].Text;
}
protected void AddRow()
{
DataTable tbleToBind = Session["TabletoBind"] as DataTable;
DataRow rowtoAdd = tbleToBind.NewRow();
rowtoAdd["EmpID"] = txtAddEmpiD.Text.Trim();
rowtoAdd["Emp Name"] = txtAddEmpname.Text.Trim();
rowtoAdd["Salary"] = txtAddSalary.Text.Trim();
rowtoAdd["Department"] = txtAddDepartment.Text.Trim();
tbleToBind.Rows.Add(rowtoAdd);
Session["TabletoBind"] = tbleToBind;
dgEmployeeDetails.DataSource = Session["TabletoBind"];
dgEmployeeDetails.DataBind();
//dgEmployeeDetails.DataSource=
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddRow();
}
protected void btnAddRowToGridView_Click(object sender, EventArgs e)
{
DataTable table = Session["TabletoBind"] as DataTable;
TextBox txtNewEmpID = new TextBox();
txtNewEmpID.ID = "txtNewEmpID";
TextBox txtNewEmpName = new TextBox();
txtNewEmpName.ID = "txtNewEmpName";
TextBox txtNewSalary = new TextBox();
txtNewSalary.ID = "txtNewSalary";
TextBox txtNewDepartment = new TextBox();
txtNewDepartment.ID = "txtNewDepartment";
txtNewEmpID.Width = 60;
txtNewEmpName.Width = 60;
txtNewSalary.Width = 60;
txtNewDepartment.Width = 60;
DataRow rowtoAdd = table.NewRow();
rowtoAdd["EmpID"] = string.Empty;
rowtoAdd["Emp Name"] = string.Empty;
rowtoAdd["Salary"] = string.Empty;
rowtoAdd["Department"] = string.Empty;
table.Rows.InsertAt(rowtoAdd, table.Rows.Count);
//table.Rows.Add(rowtoAdd);
Session["TabletoBind"] = table;
dgEmployeeDetails.DataSource = Session["TabletoBind"];
dgEmployeeDetails.DataBind();
int i = dgEmployeeDetails.Rows.Count - 1;
GridViewRow row = dgEmployeeDetails.Rows[dgEmployeeDetails.Rows.Count - 1];
row.Cells[1].Controls.Add(txtNewEmpID);
txtNewEmpID.Attributes.Add("onblur", "javascript:return FillHiddenFields(1,'" + txtNewEmpID.ClientID + "')");
row.Cells[2].Controls.Add(txtNewEmpName);
txtNewEmpName.Attributes.Add("onblur", "javascript:return FillHiddenFields(2,'" + txtNewEmpName.ClientID + "')");
row.Cells[3].Controls.Add(txtNewSalary);
txtNewSalary.Attributes.Add("onblur", "javascript:return FillHiddenFields(3,'" + txtNewSalary.ClientID + "')");
row.Cells[4].Controls.Add(txtNewDepartment);
txtNewDepartment.Attributes.Add("onblur", "javascript:return FillHiddenFields(4,'" + txtNewDepartment.ClientID + "')");
btnSetValue.Visible = true;
btnAddRowToGridView.Visible = false;
}
protected void btnSetValue_Click(object sender, EventArgs e)
{
DataTable table = Session["TabletoBind"] as DataTable;
table.Rows.RemoveAt(table.Rows.Count-1);
// btnAddRowToGridView.Visible = true;
//dgEmployeeDetails.DeleteRow(
int i = dgEmployeeDetails.Rows.Count - 2;
GridViewRow gridrow = dgEmployeeDetails.Rows[dgEmployeeDetails.Rows.Count - 1];
DataRow rowtoAdd = table.NewRow();
rowtoAdd["EmpID"] = hdnEmpId.Value;//((TextBox)(dgEmployeeDetails.Rows[dgEmployeeDetails.Rows.Count - 1].FindControl("txtNewEmpID"))).Text;
rowtoAdd["Emp Name"] = hdnEmpName.Value; //((TextBox)dgEmployeeDetails.Rows[dgEmployeeDetails.Rows.Count - 1].FindControl("txtNewEmpName")).Text;
rowtoAdd["Salary"] = hdnSalary.Value;//((TextBox)dgEmployeeDetails.Rows[dgEmployeeDetails.Rows.Count - 1].FindControl("txtNewSalary")).Text;
rowtoAdd["Department"] = hdnDepartment.Value; //((TextBox)dgEmployeeDetails.Rows[dgEmployeeDetails.Rows.Count - 1].FindControl("txtNewDepartment")).Text;
table.Rows.InsertAt(rowtoAdd, table.Rows.Count);
Session["TabletoBind"] = table;
dgEmployeeDetails.DataSource = Session["TabletoBind"];
dgEmployeeDetails.DataBind();
btnAddRowToGridView.Visible = true;
}
}
Revision number 1, Wednesday, September 23, 2009 7:32:23 PM by narpal.sr
This is not the most up to date version of this article. The most recent version can be found here.
You must Login to comment.
|
Tue, Dec 29, 2009 4:13 AM
by amanbhullar
|
Thanks for sharing code, Do explain your code
|