ASP.NET - gridview within gridview

Asked By chitra ganapathy on 02-May-11 08:41 AM
hi all, i want the code for gridview within the gridview with update, select, cancel and edit options. please give an idea
Mihir Soni replied to chitra ganapathy on 02-May-11 08:50 AM
Hello,


Here is complete tutorial for you.

Please follow the tutorial and let us know if you get stuck somewhere.

http://www.codeproject.com/KB/viewstate/GridViewWithinGridView.aspx

Hope this helps you

thank you
dipa ahuja replied to chitra ganapathy on 02-May-11 08:56 AM
check this article:

http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=83
Jitendra Faye replied to chitra ganapathy on 02-May-11 09:10 AM

The Outer GridView


To create the first (outer) grid, insert a GridView control and specify the data source (Access Database in our example). Select the columns you want to show. I selected carmaker_id and carmaker_name but the second would suffice; you have to select the primary key though, if you want to allow for update and delete operations. Name the GridView gvCars. You should already have the columns you selected as BoundFields, (select "Edit Columns"). Configure the column and set the header to "Car Maker". Up until now there was no coding required.


The Nested GridView


Because the database has two tables with a one-to-many relationship, we naturally want to show the data accordingly. This can be achieved by inserting a second GridView inside the outer one.


Using the GridView's smart tag, add a new column to the first grid and set its type to "TemplateField". Then, again using the smart tag, select "Edit Templates" to edit the item template you just created. Insert a new GridView control into it and name it gvModels. Set the "Show header" property of this grid to False so that you don't get repeated headers in every row of the outer grid. Exit the template editing by selecting "End template editing" in the smart tag.


Unfortunately there is no way of visually binding both GridViews, even in a simple one-to-many relationship. However, it is straightforward to do it through code. Start by creating an event handler for the RowDataBound event of the first GridView (gvCars). All you have to do is type a function name (I named it gvCars_rowDataBound) in the Properties window (select the lightning bolt to see the events). Visual Studio automatically creates the event handler at the Default.aspx.cs file.


Now all you have to do is associate the data you want to show in the inner grid for each row of the outer grid, like this:

    protected void gvCars_rowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv =
             (GridView)e.Row.FindControl("gvModels");
            AccessDataSource asd = new AccessDataSource(@"App_Data\SkinSample.mdb", 
"SELECT carmodel_name,carmodel_engine,carmodel_color FROM carmodel 
WHERE carmaker_id=" + ((DataRowView)e.Row.DataItem)["carmaker_id"].ToString());
            gv.DataSource = asd;
            gv.AutoGenerateColumns = false;
            BoundField bfModelName = new BoundField();
            bfModelName.DataField = "carmodel_name";
            BoundField bfEngine = new BoundField();
            bfEngine.DataField = "carmodel_engine";
            BoundField bfColor = new BoundField();
            bfColor.DataField = "carmodel_color";
            gv.Columns.Add(bfModelName);
            gv.Columns.Add(bfEngine);
            gv.Columns.Add(bfColor);
            gv.DataBind();
        }
    }
see http://www.codeproject.com/KB/aspnet/SkinSample.aspx for details.
Riley K replied to chitra ganapathy on 02-May-11 09:13 AM
Check this below article and respond with any issues u got

http://
Reena Jain replied to chitra ganapathy on 02-May-11 09:19 AM
hi,

like this you can give gridview inside the girdview

<asp:GridView ID="GridView1" runat="server"  AllowSorting="true" AutoGenerateColumns="false"
      CellPadding="4" ForeColor="#333333" GridLines="None" onsorting="GridView1_Sorting" DataKeyNames="CatID"
      onrowediting="GridView1_RowEditing">
          <RowStyle BackColor="#E3EAEB" />
          <Columns>
            <asp:TemplateField HeaderText="CategoryID" SortExpression="CatID">
              <ItemTemplate>
                <%# Eval("CatID") %>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Category" SortExpression="CatDet">
              <ItemTemplate>
                <%# Eval("CatDet") %>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Product Details">
              <ItemTemplate>
                <asp:GridView ID="grd" runat="server" AllowSorting="true"
            AutoGenerateColumns ="false" DataSource='<%#FillIG(Convert.ToString(Eval("CatID")),"","")%>'>
                  <Columns>
                    <asp:BoundField  HeaderText="ProductID" DataField ="Product_ID" SortExpression="Product_ID"/>
                    <asp:BoundField HeaderText="Product Name" DataField="Product_Name" SortExpression="Product_Name"/>
                    <asp:BoundField HeaderText="Product Price" DataField="Product_Price" SortExpression="Product_Price"/>
                  </Columns>
                </asp:GridView>
              </ItemTemplate>
            </asp:TemplateField>
          </Columns>
          <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
          <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
          <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
          <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
          <EditRowStyle BackColor="#7C6F57" />
          <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

Hope this will help you
more:
Reena Jain replied to chitra ganapathy on 02-May-11 09:20 AM
hi,

Here are two good links on this where you get the idea about nested gridview

http://www.codeforasp.net/gridview-inside-another-gridview.html
http://www.codeproject.com/KB/webforms/GridViewInsideGridView.aspx

hope this will help you
Devil Scorpio replied to chitra ganapathy on 02-May-11 03:51 PM
Hi Chitra,

The best way to walk through the presentation is to list each step with a brief explanation. Even if you are unfamiliar with GridView, smart tags, or UserControls, the detailed steps should walk you through the process:

1. Create a new Web page (or use an existing one in your project).
2. Add a GridView to that page. If the GridView is part of a whole page, explore Master pages as a way to get consistent looking pages. DataList and Repeater controls work here too.
3. Define bound columns for every element of data in your master object. (I use custom objects more often than not because I like the greater control afforded by custom controls, and I don't need to drag ADO.NET into my presentation layer.)
4. Convert one or more bound columns into a template column by clicking on the smart tag at the top-left corner of the GridView, selecting Edit columns from the smart menu, selecting the field to convert, and clicking the self-explanatory link for converting a bound column to a template column (see Figure 2).
5. Close the Fields dialog.
6. Add a UserControl to your project.
7. Click the Smart tag on the GridView again and select Edit Templates.
8. Drag and drop the UserControl from the solution explorer to the ItemTemplate, remove the existing Label and TextBox, and click Edit UserControl from the Smart tag menu (see Figure 3).
9.Add a second GridView to the UserControl and you have the basic design.

refer http://www.codeguru.com/columns/vb/article.php/c12647 for details.