Click here to Skip to main content
15,885,757 members
Articles / DropDownList
Article

Bind nested grid view and find value from control within nested grid

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL4 min read 21.4K   1  
Introduction: In this article i'm posting "How to bind nested grid view and work with nested control with nested grid view".Binding nested grid and

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Introduction: In this article i'm posting "How to bind nested grid view and work with nested control with nested grid view".

Binding nested grid and work with any control of nested grid view is something different than normal grid view. In this article i'm show how to performe update action and select item in drop down list according any values that retriving from database.

Many time we need to have nested grid view. and in many time we use drop down list with grid view, we have some item in drop down list and then we want to select a item according our retrieved value from database. And we want to update any collumn of nested grid view.so lets start.

I'm using here 3 tier architecture and i have a two seperate classes 1logiclayer and 2nd datalayer.

i bind nested grid view with parent grid view on Row_DataBound event of parent grid view. And i'm using commandArgument for identify row of nested grid view. and also select item in dropdownlist according database value.

i retrivie data from database and select item in dropdownlist in nested grid view and select item in dropdownlist.

and user also update status from select item from dropdownlist. In dropdownist i have 3 items (Open/Close/Finish).

if in database there is Open then in dropdownlist Open will be select on binding time and if thre is Finish/Close then in dropdownlist there will be Finis/Close will be select. 

User update status, for updating status he/she select status from dropdownlist and then click on update linkbutton that is place in nested grid view. on update link button i update status according task id. on that time i found task id from label that is place in nested grid view. I use commandArgument property of linkbutton for identify row. 

 

Here is my code behind file/

 

using System;
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 _Default : System.Web.UI.Page
{
    logiclayer li = new logiclayer(); // logiclayer is my business layer in 3tier architecture this is separate class in my project
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillparent();
        }
    }
    public void fillparent() // for bind parent grid view
    {
        GridView1.DataSource = li.selectparent();
        GridView1.DataBind();
        // we can also select status in dropdownlist by writing code on databound event of parent grid view.
        for (int i = 0; i < GridView1.Rows.Count; i++) // select status in drop downlist box of nested grid view
        {
            GridView childgrid = (GridView)GridView1.Rows[i].FindControl("GridView2");
            for (int m = 0; m < childgrid.Rows.Count; m++)
            {
                li.pid = Convert.ToInt32(((Label)childgrid.Rows[m].FindControl("Label4")).Text);
                li.getstatus(li);
                DropDownList childroplst = (DropDownList)childgrid.Rows[m].FindControl("DropDownList1");
                for (int k = 0; k < childroplst.Items.Count; k++)
                {
                    if (childroplst.Items[k].ToString() == li.status)
                    {
                        childroplst.Items[k].Selected = true;
                    }
                }
            }
        }
     }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)// Bind nested grid view with parent grid view
        {
            li.pid =Convert.ToInt32(( (Label)e.Row.FindControl("Label3")).Text);
            GridView childgrd = (GridView)e.Row.FindControl("GridView2"); // find nested grid view from paretn grid veiw
            childgrd.DataSource = li.fillchild(li);
            childgrd.DataBind();
          }
    }
   protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridView GridView2 = (GridView)sender;  //find nested grid view
        GridViewRow chrow = (GridViewRow)GridView2.Parent.Parent; // parent grid's row for find any control from parent grid view
        if (e.CommandName == "update")
        {
            int index = Convert.ToInt32(e.CommandArgument); //use command argument for identify row with nested grid view
            li.pid = Convert.ToInt32(((Label)GridView2.Rows[index].FindControl("Label4")).Text); // id from labe4 of nested grid view
            DropDownList childrop = ((DropDownList)GridView2.Rows[index].FindControl("DropDownList1")); // get dropdownlist box of nested grid view
            li.status = childrop.SelectedItem.Text;
            li.updatechild(li); // update status in database according task id
            //if we want to bind again then u can use disabled codes........
            //li.pid = Convert.ToInt32(((Label)chrow.FindControl("Label3")).Text);
            //GridView2.DataSource = li.fillchild(li);
            //GridView2.DataBind();

            li.pid = Convert.ToInt32(((Label)GridView2.Rows[index].FindControl("Label4")).Text);
            li.getstatus(li);// retrive status from database. li is object of logic layer
            for (int i = 0; i < childrop.Items.Count; i++) //select status in drop down list box according database.
            {
                if (childrop.Items[i].ToString() == li.status)
                {
                    childrop.Items[i].Selected = true;
                }
            }
       }
    }
}

 

**********************************************

Here is My design page .aspx 

 

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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">
    <div>
    <center>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Project">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("p_name") %>'></asp:Label>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sub Task">
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" onrowcommand="GridView2_RowCommand"
                            >
                            <Columns>
                                <asp:TemplateField HeaderText="Task Name">
                                    <ItemTemplate>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("t_name") %>'></asp:Label>
                                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("t_id") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Task Status">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="DropDownList1" runat="server">
                                            <asp:ListItem>Open</asp:ListItem>
                                            <asp:ListItem>Close</asp:ListItem>
                                            <asp:ListItem>Finish</asp:ListItem>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update" CommandArgument="<%#((GridViewRow) Container).RowIndex %>">Update</asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle BackColor="#CCCCFF" />
                        </asp:GridView>
                       
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="Maroon" ForeColor="#FFCC00" />
        </asp:GridView>
        </center>
   
    </div>
    </form>
</body>
</html>
 


 

 

 

 

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --