在 ASP.NET gridview 中显示父/子记录

发布于 2024-12-14 16:58:48 字数 342 浏览 1 评论 0原文

好吧,我似乎无法弄清楚这个问题..我有 1 个表,其中包含列 id、parentid 这允许一个选项有一个子选项。所以它会是这样的:

id  |  parentid
1   |    null
2   |    null
3   |      1 

我如何需要它在我的 gridview 中显示是这样的:

id  |  parentid
 1  |     null
 3  |      1
 2  |     null

任何想法我都将是最伟大的..以防万一我在选择上搜索并尝试了 Sql COALESCE 但那不起作用任何一个..

Ok I can't seem to figure this one out..I have 1 table which has column id, parentid
this allows an option to have a sub-option. So it would be something like this:

id  |  parentid
1   |    null
2   |    null
3   |      1 

How I need it to show in my gridview is like this:

id  |  parentid
 1  |     null
 3  |      1
 2  |     null

any ideas I would be most greatful..and just in case I have searched and tried Sql COALESCE on the select but that didn't work either..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2024-12-21 16:58:53

我会使用 ListView 或 DataList 而不是 GridView。这应该会让您走上正确的道路:

<table width="595px">
    <asp:DataList BackColor="#ffffff" id="DataList1" DataKeyField="<ID>" OnItemDataBound="DataList1_ItemDataBound" runat="server" Width="100%">     
        <ItemTemplate>
           <tr>
              <td>
                  <asp:LinkButton ID="LinkButton1" runat="server" Text="+" OnCommand="LinkButton1_Command" CommandArgument='<%#Container.ItemIndex%>'></asp:LinkButton>    
              </td>
              <td><%#Eval("<COLUMN NAME>")%></td>
              <td><%#Eval("<COLUMN NAME>")%></td>
              <td><%#Eval("<COLUMN NAME>")%></td>
           </tr>
           <asp:PlaceHolder ID="plcChildView" runat="server">
               <asp:DataList ID="DataList2" runat="server" Width="100%">
                   <ItemTemplate>
                       <tr>
                          <td><%#Eval("<CHILD OLUMN NAME>")%></td>
                          <td><%#Eval("<CHILD COLUMN NAME>")%></</td>
                          <td><%#Eval("<CHILD COLUMN NAME>")%></</td>                           
                       </tr>
                   </ItemTemplate>
               </asp:DataList>
           </asp:Panel>
        </ItemTemplate>
    </asp:DataList>
</table>

当用户单击 DataList1 中的 LinkBut​​ton/Button 时,执行如下操作:

protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    //pass index of item in command argument
    int itemIndex = Convert.ToInt32(e.CommandArgument);      

    //find the pnlChildView control
    PlaceHolder childViewHolder = (PlaceHolder)DataList1.Items[itemIndex].FindControl("plcChildView");
    if (childViewHolder != null)
    {
        //toggle visibility of childViewPanel and bind child list if panel is visible

        if (childViewHolder.Visible)
        {
            DataList childList = childViewHolder.FindControl("DataList2");
            if (childList != null)
            {
                int keyValue = (int)DataList1.DataKeys[itemIndex];

                //bind the list using DataList1 data key value
                childList.DataSource = <DATA SOURCE>; //get data using keyValue
                childList.DataBind();
            }  
        }
    }
}

I would use a ListView or DataList instead of a GridView. This should put you on the right path:

<table width="595px">
    <asp:DataList BackColor="#ffffff" id="DataList1" DataKeyField="<ID>" OnItemDataBound="DataList1_ItemDataBound" runat="server" Width="100%">     
        <ItemTemplate>
           <tr>
              <td>
                  <asp:LinkButton ID="LinkButton1" runat="server" Text="+" OnCommand="LinkButton1_Command" CommandArgument='<%#Container.ItemIndex%>'></asp:LinkButton>    
              </td>
              <td><%#Eval("<COLUMN NAME>")%></td>
              <td><%#Eval("<COLUMN NAME>")%></td>
              <td><%#Eval("<COLUMN NAME>")%></td>
           </tr>
           <asp:PlaceHolder ID="plcChildView" runat="server">
               <asp:DataList ID="DataList2" runat="server" Width="100%">
                   <ItemTemplate>
                       <tr>
                          <td><%#Eval("<CHILD OLUMN NAME>")%></td>
                          <td><%#Eval("<CHILD COLUMN NAME>")%></</td>
                          <td><%#Eval("<CHILD COLUMN NAME>")%></</td>                           
                       </tr>
                   </ItemTemplate>
               </asp:DataList>
           </asp:Panel>
        </ItemTemplate>
    </asp:DataList>
</table>

And when the user clicks the LinkButton/Button in DataList1, do something like this:

protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    //pass index of item in command argument
    int itemIndex = Convert.ToInt32(e.CommandArgument);      

    //find the pnlChildView control
    PlaceHolder childViewHolder = (PlaceHolder)DataList1.Items[itemIndex].FindControl("plcChildView");
    if (childViewHolder != null)
    {
        //toggle visibility of childViewPanel and bind child list if panel is visible

        if (childViewHolder.Visible)
        {
            DataList childList = childViewHolder.FindControl("DataList2");
            if (childList != null)
            {
                int keyValue = (int)DataList1.DataKeys[itemIndex];

                //bind the list using DataList1 data key value
                childList.DataSource = <DATA SOURCE>; //get data using keyValue
                childList.DataBind();
            }  
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文