Click here to Skip to main content
15,885,309 members
Articles / Web Development / ASP.NET

Gridview inside Gridview in ASP.NET C#

Rate me:
Please Sign up or sign in to vote.
4.78/5 (19 votes)
9 Jun 2011CPOL1 min read 194K   8.4K   42  
Gridview inside Gridview in ASP.NET C# or Nested Gridview and Update, Delete from Gridview
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NestedGridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadAll();
        }
    }

    private void loadAll()
    {
        DataSet ds = new DataSet();
        ds = query.selectAll();
        //DataSet ds1 = new DataSet();
        //ds1 = query.selectAll();
        //ds1.Merge(ds);

        //  DataTable dt = new DataTable();
        // dt = query.selectAllTBL();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        loadAll();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        loadAll();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        string lblID = ((Label)row.FindControl("lblID")).Text;
        string txt = ((TextBox)row.FindControl("txt")).Text;
        GridView1.EditIndex = -1;
        int n = Class1.nonquery1(lblID, txt);
        loadAll();
        // int n = query.update(lblID, txt);
        //if (n == 0)
        //{

        //}
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Show")
        {
            int RowIndex = Convert.ToInt32((e.CommandArgument).ToString());
            Button btn = (Button)GridView1.Rows[RowIndex].FindControl("btnShow");
           // Label lblID = (Label)GridView1.Rows[RowIndex - 1].FindControl("Label1");
           // int row = Convert.ToInt32(lblID.Text);
            if (btn.Text == "Expand")
            {

                //  Label lblID = (Label)gvDiscMaster.Rows[RowIndex].FindControl("lblID");
                GridView gv = (GridView)GridView1.Rows[RowIndex ].FindControl("GridView2");

                long id = long.Parse(((Label)GridView1.Rows[RowIndex].FindControl("lblID")).Text);
                DataSet ds1 = new DataSet();
                ds1 = query.selectwithId(id);
                gv.DataSource = ds1;
                gv.DataBind();
                btn.Text = "Collapse";
            }
            else if (btn.Text == "Collapse")
            {
                GridView gv = (GridView)GridView1.Rows[RowIndex].FindControl("GridView2");

                long id = long.Parse(e.CommandArgument.ToString());
                //DataSet ds1 = new DataSet();
               // ds1 = query.selectwithId(id);
                gv.DataSource = null;
                gv.DataBind();
                btn.Text = "Expand";
            }

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer India
India India
http://devcorners.com/
Total DotNet/Programming Solution

I am Prasanta Banerjee. I am an Asp.Net Developer. My site: http://devcorners.com/
Email: prasanta.it@hotmail.com
If any body wants to prepare for interview http://guru-code.blogspot.com/ is the good site.

Comments and Discussions