如何在另一个Gridview中嵌入Gridview

时间:2014-08-12 11:16:47

标签: c# asp.net gridview

我正在开发一个包含嵌套Gridview(总共3个Gridview)的Web应用程序。

我的要求:     我需要的是,基于文本框中的值,3个网格视图应该被绑定。它应该显示一个接一个,如下面的示例图像所示

enter image description here

我的问题是只有第一个网格是绑定而不是子网格。如下所示。 enter image description here

这是我的aspx代码:

<asp:UpdatePanel ID="UPDestinationGrid" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="dgDestination1" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                            BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                            AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                            Width="700px" OnRowDataBound="dgDestination_OnRowDataBound"> 
                            <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                            <SelectedRowStyle Font-Bold="True" ForeColor="Black" BackColor="Snow"></SelectedRowStyle>
                            <EditRowStyle BackColor="AntiqueWhite"></EditRowStyle>
                            <PagerStyle BackColor="#FDE9CB" ForeColor="#003399" HorizontalAlign="Right"
                                Font-Size="Small" Font-Bold="true" />
                            <AlternatingRowStyle BackColor="Snow"></AlternatingRowStyle>
                            <RowStyle ForeColor="#000066" BackColor="Snow"></RowStyle>
                            <HeaderStyle Font-Size="XX-Small" Font-Bold="True" Height="10px" ForeColor="#000000"
                            BackColor="#FFDBA6"></HeaderStyle>
            <Columns>
                <asp:TemplateField ItemStyle-Width="20px">
                <ItemTemplate>
                        <a href="JavaScript:divexpandcollapse('div<%# Eval("Port") %>');">
                            <img id="imgdiv<%# Eval("Port") %>" width="9px" border="0" 
                    </ItemTemplate>
                    <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Port">
                <ItemTemplate>
                    <asp:Label ID="lblPortID" runat="server" Visible="false" Text='<%#DataBinder.Eval(Container.DataItem,
                                                                            "Port") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="mROU_NUPKId" HeaderText="mROU_NUPKId" Visible="False">
                </asp:BoundField>
                <asp:BoundField DataField="mROD_mROU_NUPKId" HeaderText="mROD_mROU_NUPKId" Visible="False">
                </asp:BoundField>
                <asp:BoundField DataField="Port" HeaderText="Ports"></asp:BoundField>
                <asp:TemplateField HeaderText="Sailing">
                    <ItemTemplate>
                        <tr>
                            <td colspan="100%">
                                <div id="div<%# Eval("Port") %>"  style="overflow:auto; display:none;
                                                        position: relative; left: 15px; overflow: auto">
                                    <asp:GridView ID="dgSailing" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                                                    BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                                                    AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                                                    Width="700px" DataKeyNames="Origin"
                                                    OnRowDataBound="dgSailing_OnRowDataBound">
                                                    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                                                    <SelectedRowStyle Font-Bold="True" ForeColor="Black" BackColor="Snow"></SelectedRowStyle>
                                                    <EditRowStyle BackColor="AntiqueWhite"></EditRowStyle>
                                                    <PagerStyle BackColor="#FDE9CB" ForeColor="#003399" HorizontalAlign="Right"
                                                    Font-Size="Small" Font-Bold="true" />
                                                    <AlternatingRowStyle BackColor="Snow"></AlternatingRowStyle>
                                                    <RowStyle ForeColor="#000066" BackColor="Snow"></RowStyle>
                                                    <HeaderStyle Font-Size="XX-Small" Font-Bold="True" Height="10px" ForeColor="#000000"
                                                    BackColor="#FFDBA6"></HeaderStyle>
                                        <Columns>
                                            <asp:TemplateField ItemStyle-Width="20px">
                                                <ItemTemplate>
                                                    <a href="JavaScript:divexpandcollapse('div1<%# Eval("ArrivalDate") %>');">
                                                    <img id="imgdiv1<%# Eval("ArrivalDate") %>" width="9px" border="0" src="AppImages/plus.gif"
                                                                alt="" /></a>                       
                                                </ItemTemplate>
                                                <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
                                                </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Salary ID" Visible="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblArrivaldate" Visible="false" runat="server" Text='<%#DataBinder.Eval
                                                                                                (Container.DataItem, "ArrivalDate") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>

                                            <asp:BoundField DataField="Origin" HeaderText="Origin " SortExpression="Origin"></asp:BoundField>
                                            <asp:BoundField DataField="Destination" SortExpression="Destination" HeaderText="Destination"></asp:BoundField>
                                            <asp:BoundField DataField="ID" HeaderText="ID" Visible="False"></asp:BoundField>
                                            <asp:BoundField DataField="DepartureDate" HeaderText="Departure Date" SortExpression="DepartureDate" dataformatstring="{0:dd-MM-yyyy}"></asp:BoundField>
                                            <%--<asp:BoundField DataField="ArrivalDate" HeaderText="Arrival Date" SortExpression="ArrivalDate" dataformatstring="{0:dd-MM-yyyy}"></asp:BoundField>--%>
                                            <asp:BoundField DataField="Carrier" HeaderText="Carrier" SortExpression="Carrier"></asp:BoundField>
                                            <asp:BoundField DataField="VesselName" HeaderText="Vessel" SortExpression="VesselName"></asp:BoundField>
                                            <asp:BoundField DataField="VoyageNo" HeaderText="Voyage No" SortExpression="VoyageNo"></asp:BoundField>
                                            <asp:BoundField DataField="TransitTime" HeaderText="TransitTime" SortExpression="TransitTime"  DataFormatString="{0:n}"></asp:BoundField>
                                            <asp:BoundField DataField="DCFDate" HeaderText="DCF Date" SortExpression="DCFDate" dataformatstring="{0:dd-MM-yyyy}"></asp:BoundField>
                                            <asp:BoundField DataField="CCFDate" HeaderText="CCF Date" SortExpression="CCFDate" dataformatstring="{0:dd-MM-yyyy}"></asp:BoundField>
                                            <%-- <asp:TemplateField>
                                                <ItemTemplate>
                                                    <tr>
                                                        <td colspan="100%">
                                                            <div id="div1<%# Eval("ArrivalDate") %>"  style="overflow:auto; display:none;
                                                                                        position: relative; left: 15px; overflow: auto">
                                                                <asp:GridView ID="dgJobgrid" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                                                                                    BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                                                                                    AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                                                                                    Width="700px" OnRowDataBound="dgJobgrid_OnRowDataBound" >
                                                                                    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                                                                                    <SelectedRowStyle Font-Bold="True" ForeColor="Black" BackColor="Snow"></SelectedRowStyle>
                                                                                    <EditRowStyle BackColor="AntiqueWhite"></EditRowStyle>
                                                                                    <PagerStyle BackColor="#FDE9CB" ForeColor="#003399" HorizontalAlign="Right" Font-Size="Small" Font-Bold="true" />
                                                                                    <AlternatingRowStyle BackColor="Snow"></AlternatingRowStyle>
                                                                                    <RowStyle ForeColor="#000066" BackColor="Snow"></RowStyle>
                                                                                    <HeaderStyle Font-Size="XX-Small" Font-Bold="True" Height="10px" ForeColor="#000000"
                                                                                    BackColor="#FFDBA6"></HeaderStyle>


                                                                    <Columns>
                                                                    <asp:TemplateField HeaderText="SNO">
                                                                        <HeaderStyle Width="5%" Font-Underline="true" />
                                                                        <ItemTemplate>
                                                                                <%# Container.DataItemIndex + 1%>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField DataField="tBLG_NUPKId" HeaderText="tBLG_NUPKId" Visible="False"></asp:BoundField>
                                                                    <asp:BoundField DataField="PortOfLoadName" HeaderText="Origin"></asp:BoundField>
                                                                    <asp:BoundField DataField="MasterNo" HeaderText="JobNo"></asp:BoundField>
                                                                    <asp:BoundField DataField="Volume" HeaderText="Volume"></asp:BoundField>
                                                                    <asp:BoundField DataField="Volumetype" HeaderText="Volumetype"></asp:BoundField>
                                                                    </asp:GridView>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </ItemTemplate>
                                            </asp:TemplateField>--%>
                                        </Columns>
                                    </asp:GridView>
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>    
            </Columns>
            <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" >
            </PagerStyle>
        </asp:GridView>
    </contenttemplate>
</asp:UpdatePanel> 

这是.cs代码:

private void BindDestinationgrid()
{
    try
    {
        UPDestinationGrid.Update();
        UPDestinationGrid.Update();
        if (PortLID.Text == "" || PortLID.Text == "0")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('Select The Loading Port');", true);
        }
        else if (PortDID.Text == "" || PortDID.Text == "0")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('Select The Destination Port');", true);
        }
        else if (TxtArrivalDate.Text=="")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('Select The ArrivalDate');", true);
        }

        if (PortLID.Text != "" && PortLID.Text != "0")
        {
            using (LQMasterRouteDetailDataContext DB = new LQMasterRouteDetailDataContext())
            {
                var Result = from C in DB.VW_MasterRoutes
                                where C.mROU_NUIsActive == 1 && C.Origin == PortLName.Text && C.Destination == PortDName.Text
                                select new { mROU_NUPKId = C.mROU_NUPKId, mROD_mROU_NUPKId = C.mROD_mROU_NUPKId,  Port = C.Port };
                dgDestination1.DataSource = Result;
                dgDestination1.DataBind();
                int Count = Result.Count();
                if (Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('No Port found');", true);
                }
                //BindSailinggrid();
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('Please Select the Loading Port');", true);
        }
    }
    catch (Exception ex)
    {

    }
}

protected void dgDestination_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        UPDestinationGrid.Update();
        UpGeneralCondrols.Update();
        Label lblPortID = (Label)e.Row.FindControl("lblPortID");
        GridView dgSailing = (GridView)e.Row.FindControl("dgSailing");
        string txtempid = lblPortID.Text;
        if (TxtArrivalDate.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('Please Select the Date');", true);
        }
        else if (TxtArrivalDate.Text != "" && PortLName.Text !="" && txtempid != "")
        {

            using (LQMasterSailingScheduleDataContext DB = new LQMasterSailingScheduleDataContext())
            {
                var Result = from C in DB.VW_MasterSailingScheduleViews
                                where C.mVSS_NUIsActive == 1 && C.ArrivalDate == mobjGenlib.Getdate(TxtArrivalDate.Text) && C.Origin == PortLName.Text && C.Destination ==txtempid
                                select new
                                {
                                    Origin = C.Origin,
                                    Destination = C.Destination,
                                    DepartureDate = C.DepartureDate,
                                    ArrivalDate = C.ArrivalDate,
                                    Carrier = C.Carrier,
                                    VesselName = C.VesselName,
                                    VoyageNo = C.VoyageNo,
                                    TransitTime = C.TransitTime,
                                    DCFDate = C.DCFDate,
                                    CCFDate = C.CCFDate,
                                    ID = C.mVSS_NUPKId
                                };

                dgSailing.DataSource = Result;
                dgSailing.DataBind();
                int Count = Result.Count();
                if (Count == 0)
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('No Vessel found');", true);
                }
            }    
        }
    }
}

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)