DataTable.aspx Page code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lblCategoryName" runat="server" Text="Category Name"> </asp:Label>
<asp:TextBox ID="txtCategoryName" runat="server" oncopy="return false" oncut="return false"
onpaste="return false"></asp:TextBox>
<br />
<asp:Label ID="lblSubCategoryName" runat="server" Text="SubCategory Name"></asp:Label>
<asp:TextBox ID="txtSubCategoryName" runat="server" oncopy="return false" oncut="return false"
onpaste="return false"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<br />
<asp:GridView runat="server" ID="gvCategory" AutoGenerateColumns="false" OnRowCommand="gvCategory_RowCommand" DataKeyNames="RowSerialNo">
<AlternatingRowStyle CssClass="even" />
<EmptyDataTemplate>
<asp:Label ID="lblNoDataFound" runat="server" Text="Category Not Found."></asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<ItemStyle Width="35%" />
<HeaderTemplate>
<asp:Label ID="lblCategory" runat="server" Text="Category" Font-Bold="true"> </asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblAnsCategory" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Width="35%" />
<HeaderTemplate>
<asp:Label ID="lblSubCategory" runat="server" Text="SubCategory" Font-Bold="true"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblAnsSubCategory" runat="server" Text='<%# Eval("SubCategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Width="30%" />
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkCategoryEdit" runat="server" Text="Edit" CommandName="SelectRec"
CommandArgument='<%#Eval("RowSerialNo")%>' CausesValidation="false"></asp:LinkButton> <asp:LinkButton
ID="lnkCategoryDelete" runat="server" Text="X" CommandArgument='<%#Eval("RowSerialNo")%>'
CommandName="DeleteRec" CausesValidation="false" OnClientClick='javascript:return confirm("Are you sure want to delete this entry?");'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
DataTable.aspx.cs Page code
namespace Datatable
{
public partial class Test : System.Web.UI.Page
{
Int16 RecSerialNo
{
get
{
if (ViewState["RecSerialNo"] == null)
{
ViewState["RecSerialNo"] = 1;
}
return System.Convert.ToInt16(ViewState["RecSerialNo"].ToString());
}
set
{
ViewState["RecSerialNo"] = value;
}
}
DataTable pageDatatable;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
CreateDataTable();
}
private void CreateDataTable()
{
pageDatatable = new DataTable();
DataColumn[] keys = new DataColumn[1];
DataColumn col1 = new DataColumn("CategoryName");
DataColumn col2 = new DataColumn("SubCategoryName");
DataColumn col5 = new DataColumn("RowSerialNo");
pageDatatable.Columns.Add(col1);
pageDatatable.Columns.Add(col2);
pageDatatable.Columns.Add(col5);
keys[0] = col5;
pageDatatable.PrimaryKey = keys;
ViewState["PagedataTable"] = pageDatatable;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (ViewState["PagedataTable"] != null)
{
DataTable table = (DataTable)ViewState["PagedataTable"];
DataRow row = table.NewRow();
row["CategoryName"] = txtCategoryName.Text;
row["SubCategoryName"] = txtSubCategoryName.Text;
row["RowSerialNo"] = RecSerialNo;
if (ViewState["Id"] != null)
{
table.Rows.Find(System.Convert.ToInt64(ViewState["Id"].ToString())).Delete();
ViewState["Id"] = null;
}
table.Rows.Add(row);
table.AcceptChanges();
ViewState["PagedataTable"] = table;
gvCategory.DataSource = (DataTable)ViewState["PagedataTable"];
gvCategory.DataBind();
txtCategoryName.Text = string.Empty;
txtSubCategoryName.Text = string.Empty;
RecSerialNo++;
}
}
protected void gvCategory_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataTable dt = (DataTable)ViewState["PagedataTable"];
if (e.CommandName == "DeleteRec")
{
string Id = e.CommandArgument.ToString();
dt.Rows.Find(System.Convert.ToInt64(Id)).Delete();
dt.AcceptChanges();
ViewState["PagedataTable"] = dt;
gvCategory.DataSource = ViewState["PagedataTable"];
gvCategory.DataBind();
}
if (e.CommandName == "SelectRec")
{
DataRow dr = dt.Rows.Find(System.Convert.ToInt64(e.CommandArgument.ToString()));
txtCategoryName.Text = dr[0].ToString();
txtSubCategoryName.Text = dr[1].ToString();
ViewState["Id"] = e.CommandArgument.ToString();
}
}
}
}
1 comments:
Thanks a lot it was extremely helpfullll
Post a Comment