如何在asp.net中填充和显示子gridview

时间:2017-03-29 20:20:34

标签: c# asp.net gridview

我正在开发一个asp.net C#项目。我创建了一个嵌套的gridview(gv)。子gv数据的主键是父gv的DatakeyNames值。 DatakeyNames设置为SchemeId。我遇到的问题是,如果我使用DatakeyNames的SchemeId从代码隐藏填充子gv,则不会显示子gv。我尝试使用带有ItemTemplate的隐藏字段来获取值。这个问题是在网格视图的末尾显示一个空列。为了避免这种情况,我尝试将隐藏字段添加为另一个ItemTemplate的一部分,然后也不显示子gv。然后我在包含隐藏字段的ItemTemplate上尝试了visible = False,但仍然显示了一个空列。这很奇怪。非常感谢任何帮助。

        <asp:GridView ID="grdParent" BackColor="#f1f1f1" CellPadding="0" CellSpacing="0"    
        AutoGenerateColumns=false DataKeyNames="SchemeId"             
        runat="server" OnRowDataBound="grdParent_RowDataBound" Width="80%" OnRowCreated="grdParent_OnRowCreated" >
        <AlternatingRowStyle BackColor="White" />
        <RowStyle Font-Size="12px" VerticalAlign="Top" Height="30px" />           
        <HeaderStyle BackColor="#57668A" ForeColor="#d6d6d6" Font-Bold="True" Font-Size="14px" Height="35px"/>

        <Columns>
            <asp:TemplateField>
                <ItemTemplate>

                    <a href="javascript:expandcollapse('div<%# Eval("PlanedScheduleId") %>', 'one');">
                        <img id="imgdiv<%# Eval("PlanedScheduleId") %>" alt="Click to show/hide Orders for Customer <%# Eval("PlanedScheduleId") %>" width="10px" height="10px" src="images/Minus-26.png"/>
                    </a>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Date" SortExpression="Date">
                <ItemTemplate>
                    <asp:Label ID="lblDate" Text='<%# Eval("Date") %>' runat="server"></asp:Label>
                </ItemTemplate> 
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />                  
            </asp:TemplateField>
            <asp:TemplateField >
                <ItemTemplate >
                    <asp:HiddenField ID="hfSchemeId" runat="server" Value='<%# Eval("SchemeId") %>' />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <tr>
                        <td colspan="100%">
                            <div class="divBackground" id="div<%# Eval("PlanedScheduleId") %>" style="position:relative;OVERFLOW: auto; WIDTH: 97%;padding:0px 20px 20px 31px" >
                                <asp:GridView ID="grdChild" BackColor="White" Width=100% Font-Size=X-Small
                                    AutoGenerateColumns=false Font-Names="Verdana" runat="server" DataKeyNames="SchemeId"                                       
                                    BorderStyle=Double BorderColor="#57668A">
                                    <RowStyle Font-Size="12px" VerticalAlign="Top" height="25px" />           
                                    <HeaderStyle BackColor="#B0C4DE" ForeColor="Black" Font-Bold="True" Font-Size="12px" Height="30px"/>
                                    <AlternatingRowStyle BackColor="White" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="Start Date" >
                                            <ItemTemplate>
                                                <asp:Label ID="lblOrderID" Text='<%# Eval("StartDate") %>' runat="server"></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Start Time">
                                            <ItemTemplate><%# Eval("StartTime")%></ItemTemplate>
                                           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        </asp:TemplateField>

                                        <asp:TemplateField HeaderText="End Date" >
                                            <ItemTemplate>
                                                <asp:Label ID="Label2" Text='<%# Eval("EndDate") %>' runat="server"></asp:Label>
                                            </ItemTemplate>
                                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="End Time">
                                            <ItemTemplate><%# Eval("EndTime")%></ItemTemplate>
                                           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        </asp:TemplateField>


                                    </Columns>
                               </asp:GridView>
                            </div>
                         </td>
                    </tr>
                </ItemTemplate>                
            </asp:TemplateField>                
        </Columns>
    </asp:GridView>

   protected void grdParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int SchemeId = Convert.ToInt32(grdParent.DataKeys[e.Row.RowIndex].Values[0]);
            GridView gv = new GridView();
            gv = (GridView)row.FindControl("grdChild");
            gv.DataSource = ChildDataSource(SchemeId.ToString());
            gv.DataBind();

        }            
    }

0 个答案:

没有答案