Search This Blog

Friday, June 7, 2013

gridview inside gridview in asp.net

       
             <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Relation.aspx.cs" Inherits="Relation" %>
<head runat="server">
    <title>Sales Report</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="False" CellPadding="4"
            Width="100%" OnRowDataBound="gvParent_RowDataBound" OnRowCommand="gvParent_RowCommand"
            ForeColor="Black" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
            BorderWidth="3px" CellSpacing="2">
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="btnImage" runat="server" ImageUrl="~/Images/plus.gif" CommandName="expand" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="InvoiceNo" HeaderText="Invoice No."></asp:BoundField>
                <asp:BoundField DataField="InvoiceDate" HeaderText="Invoice Date"></asp:BoundField>
                <asp:BoundField DataField="CustomerName" HeaderText="Customer Name"></asp:BoundField>
                <asp:BoundField DataField="TotalAmount" HeaderText="Total Amount"></asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        </td></tr>
                        <tr>
                            <td>
                            </td>
                            <td colspan="4">
                                <asp:GridView ID="gvChild" runat="server" Width="100%" Visible="False" AutoGenerateColumns="False"
                                    DataSource='<%# GetChildRelation(Container.DataItem,"Relation") %>' BackColor="White"
                                    BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
                                    GridLines="Vertical">
                                    <AlternatingRowStyle BackColor="#CCCCCC" />
                                    <Columns>
                                        <asp:BoundField DataField="Item" HeaderText="Item"></asp:BoundField>
                                        <asp:BoundField DataField="Qty" HeaderText="Quantity"></asp:BoundField>
                                        <asp:BoundField DataField="Price" HeaderText="Price"></asp:BoundField>
                                        <asp:BoundField DataField="Amount" HeaderText="Total"></asp:BoundField>
                                    </Columns>
                                    <FooterStyle BackColor="#CCCCCC" />
                                    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                                    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                                    <SortedAscendingHeaderStyle BackColor="#808080" />
                                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                                    <SortedDescendingHeaderStyle BackColor="#383838" />
                                </asp:GridView>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <RowStyle BackColor="White" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#808080" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView>
    </div>
    </form>
</body>
</html> 
           
           
       
       
       
   

   




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace AJAX_enabled_Web_Service
{
    public partial class GirdInsideGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = new DataSet();
                ds = GetData();
                ds.Tables[0].TableName = "Parent";
                ds.Tables[1].TableName = "Child";
                DataRelation dr = new DataRelation("Relation", ds.Tables["Parent"].Columns["project_id"], ds.Tables["Child"].Columns["Project_ID"], false);
                dr.Nested = true;
                ds.Relations.Add(dr);
                gvParent.DataSource = ds;
                gvParent.DataMember = "Parent";
                gvParent.DataBind();
            }

        }
        protected DataView GetChildRelation(object pDataItem, string pRelation)
        {
            DataRowView pvoDataRowView;
            pvoDataRowView = (DataRowView)pDataItem;
            if (pvoDataRowView != null)
                return pvoDataRowView.CreateChildView(pRelation);
            else
                return null;
        }

        private DataSet GetData()
        {
            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = @"BEGIN select top 20 project_id,descr,engagement_manager,account_manager from synprod.tim_project where active_status='a' order by project_id desc; SELECT [NAME], [userid], [empid], [DOJ], [Project_ID], [ProjectName], [reporting_manager_id], [RMNAME], [delivery_director], [DD] FROM [GetEmpInformation]  END";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Close();
            return ds;
        }


        protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "expand")
            {
                int pviIndex;
                pviIndex = Convert.ToInt16(e.CommandArgument.ToString());
                ImageButton btn = new ImageButton();
                btn = (ImageButton)gvParent.Rows[pviIndex].FindControl("btnImage");
                if (btn.ImageUrl.ToLower().Contains("plus.gif"))
                {
                    btn.ImageUrl = "images/minus.gif";
                    ((GridView)gvParent.Rows[pviIndex].FindControl("gvChild")).Visible = true;
                }
                else
                {
                    btn.ImageUrl = "images/plus.gif";
                    ((GridView)gvParent.Rows[pviIndex].FindControl("gvChild")).Visible = false;
                }
            }
        }

        protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    ImageButton btn = new ImageButton();
                      btn = (ImageButton)e.Row.Cells[0].FindControl("btnImage");
                    btn.CommandArgument = e.Row.RowIndex.ToString();
                }
                catch(Exception ex)
                {
                   // ///throw ex;
                }
            }
        }


     
    }
}

No comments:

Post a Comment