Wednesday, 10 July 2013

Nested GridView in asp.net

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default16" %><%
@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"Namespace="System.Web.UI" TagPrefix="asp" %><!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>Untitled Page</title></
head><
body><form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager><asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate><asp:GridView ID="Gridview1" runat="server" BorderStyle="Double" BorderColor="Gold" BackColor="AliceBlue" ShowFooter="true" AutoGenerateColumns="false"> <HeaderStyle BackColor="Green" Font-Bold="true" ForeColor="White" /><Columns><asp:BoundField DataField="RowNumber" HeaderText="Row Number" /><asp:TemplateField HeaderText="Main"><ItemTemplate><table><tr><td><asp:TextBox ID="txt1" runat="server"></asp:TextBox></td><td><asp:TextBox ID="txt2" runat="server"></asp:TextBox></td><td><asp:TextBox ID="txt3" runat="server"></asp:TextBox></td></tr></table><hr /><table><tr><td><asp:GridView ID="gvChildGrid" runat="server" ShowFooter="true" AutoGenerateColumns="false"BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="250px"><HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /><RowStyle BackColor="#E1E1E1" /><AlternatingRowStyle BackColor="White" /><HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /><Columns><asp:BoundField DataField="RowNumber" HeaderText="Row Number" /><asp:TemplateField HeaderText="sub"><ItemTemplate><table><tr><td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td><td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td><td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td><td></td></tr></table></ItemTemplate><FooterTemplate><asp:Button ID="ButtonAdd" OnClick="innergridclick" CommandName="<%# Container.DataItemIndex %>"runat="server" Text="Add inner Row" /></FooterTemplate></asp:TemplateField></Columns></asp:GridView></td></tr></table></ItemTemplate><FooterTemplate><asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" /></FooterTemplate></asp:TemplateField></Columns></asp:GridView></ContentTemplate></asp:UpdatePanel></form></
body></
html>using System;using System.Collections;using System.Configuration;using System.Data;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;public partial class Default16 : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){

if (!Page.IsPostBack){
SetInitialRow();
}
}

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)));dr = dt.NewRow();
dr[
"RowNumber"] = 1;dr[
"Column1"] = string.Empty;dr[
"Column2"] = string.Empty;dr[
"Column3"] = string.Empty;dt.Rows.Add(dr);
ViewState[
"CurrentTable"] = dt;Gridview1.DataSource = dt;
Gridview1.DataBind();
SetInitialRow2();
}

private void AddNewRowToGrid(){

int rowIndex = 0;setgridviewvalues();

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++){

TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt3");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;rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState[
"CurrentTable"] = dtCurrentTable;Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}

else{
Response.Write(
"ViewState is null");}
SetPreviousData();
SetInitialRow2();
}

public void setgridviewvalues(){

if (ViewState["CurrentTable2"] != null){

DataSet dtset = (DataSet)ViewState["CurrentTable2"];
int Dtcount = dtset.Tables.Count;
for (int rowIndex = 0; rowIndex < Gridview1.Rows.Count; rowIndex++){

int rowcount = 0;
DataTable dtCurrentTable = dtset.Tables[rowIndex];
DataRow drCurrentRow = null;
GridView Grdview = (GridView)Gridview1.Rows[rowIndex].Cells[1].FindControl("gvChildGrid");
if (dtCurrentTable.Rows.Count > 0){

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++){

TextBox box1 = (TextBox)Grdview.Rows[rowcount].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Grdview.Rows[rowcount].Cells[1].FindControl("TextBox2");
TextBox box3 = (TextBox)Grdview.Rows[rowcount].Cells[1].FindControl("TextBox3");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;rowcount++;
}
ViewState[
"CurrentTable2"] = dtset;}
}
}
}

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("txt1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt3");box1.Text = dt.Rows[i][
"Column1"].ToString();box2.Text = dt.Rows[i][
"Column2"].ToString();box3.Text = dt.Rows[i][
"Column3"].ToString();rowIndex++;
}
}
}
}

protected void ButtonAdd_Click(object sender, EventArgs e){
AddNewRowToGrid();
}

protected void innergridclick(object sender, EventArgs e){

Button b1 = (Button)sender;
GridViewRow gRow = (GridViewRow)b1.Parent.Parent.Parent.Parent.Parent.Parent;
int Rindex = gRow.RowIndex;AddNewRowToGrid2(Rindex);
}

private void SetInitialRow2(){

DataSet dtset;
if (ViewState["CurrentTable2"] != null){
dtset = (
DataSet)ViewState["CurrentTable2"];
int Dtcount = dtset.Tables.Count;}

else{
dtset =
new DataSet();}

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)));dr = dt.NewRow();
dr[
"RowNumber"] = 1;dr[
"Column1"] = string.Empty;dr[
"Column2"] = string.Empty;dr[
"Column3"] = string.Empty;dt.Rows.Add(dr);
dtset.Tables.Add(dt);
ViewState[
"CurrentTable2"] = dtset;
int rowIndex = 0;
for (rowIndex = 0; rowIndex < Gridview1.Rows.Count; rowIndex++){

DataTable dtCurrentTable = dtset.Tables[rowIndex];
GridView Grdview = (GridView)Gridview1.Rows[rowIndex].Cells[1].FindControl("gvChildGrid");Grdview.DataSource = dtCurrentTable;
Grdview.DataBind();
SetPreviousData2(rowIndex);
}
}

private void AddNewRowToGrid2(int rowIndex){

int rowcount = 0;
if (ViewState["CurrentTable2"] != null){

DataSet dtset = (DataSet)ViewState["CurrentTable2"];
int Dtcount = dtset.Tables.Count;
DataTable dtCurrentTable = dtset.Tables[rowIndex];
DataRow drCurrentRow = null;
GridView Grdview = (GridView)Gridview1.Rows[rowIndex].Cells[1].FindControl("gvChildGrid");
if (dtCurrentTable.Rows.Count > 0){

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++){

TextBox box1 = (TextBox)Grdview.Rows[rowcount].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Grdview.Rows[rowcount].Cells[1].FindControl("TextBox2");
TextBox box3 = (TextBox)Grdview.Rows[rowcount].Cells[1].FindControl("TextBox3");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;rowcount++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState[
"CurrentTable2"] = dtset;Grdview.DataSource = dtCurrentTable;
Grdview.DataBind();
}
}

else{
Response.Write(
"ViewState is null");}
SetPreviousData2(rowIndex);
}

private void SetPreviousData2(int dTindex){

int rowIndex = 0;
if (ViewState["CurrentTable2"] != null){

DataSet dset = (DataSet)ViewState["CurrentTable2"];
DataTable dt = dset.Tables[dTindex];
if (dt.Rows.Count > 0){

GridView Grdview = (GridView)Gridview1.Rows[dTindex].Cells[1].FindControl("gvChildGrid");
for (int i = 0; i < dt.Rows.Count; i++){

TextBox box1 = (TextBox)Grdview.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Grdview.Rows[rowIndex].Cells[1].FindControl("TextBox2");
TextBox box3 = (TextBox)Grdview.Rows[rowIndex].Cells[1].FindControl("TextBox3");box1.Text = dt.Rows[i][
"Column1"].ToString();box2.Text = dt.Rows[i][
"Column2"].ToString();box3.Text = dt.Rows[i][
"Column3"].ToString();rowIndex++;
}
}
}
}
}

8 comments:

  1. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love

    java training in chennai | java training in USA

    selenium training in chennai

    ReplyDelete
  2. Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
    Blueprism training in Pune

    Blueprism training in Chennai

    ReplyDelete
  3. Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  4. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    Android Training in Chennai | Best Android Training in Chennai
    Matlab Training in Chennai | Best Matlab Training in Chennai
    Best AWS Training in Chennai | AWS Training in Chennai
    Selenium Training in Chennai | Best Selenium Training in chennai
    Devops Course Training in Chennai | Best Devops Training in Chennai

    ReplyDelete
  5. Really useful information. Thank you so much for sharing. It will help everyone. Keep Post.

    Best SAP Training in Bangalore for SAP, we provide the sap training project with trainers having more than 5 Years of sap training experience, we also provide 100% placement support.

    ReplyDelete