【问题标题】:Once clicking the select button of a row, a gridview appears between that row and the rest of the gridview. Possible?一旦单击一行的选择按钮,就会在该行和网格视图的其余部分之间出现一个网格视图。可能的?
【发布时间】:2012-04-10 18:04:02
【问题描述】:

我想让用户单击 400 行网格视图的第 250 行的选择按钮。当他们点击它时,另一个 3x12 的 gridview 出现在该行下方,然后其他 150 行出现在该行下方。这是可能吗?我想我可以创建一个完整的其他 div,它将具有三个 gridview 输出,具体取决于 所选行的索引。

开头是:

Gridview 行 1-400

那么选择第350行之后就是:

Gridview 行 1-350


第 350 行信息的网格视图


Gridview 行 351-400。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这绝对是可能的,但我会使用ListViewDataList 作为您的父容器,因为使用GridView,您必须将子列表放在一个列中,这看起来很难看。这应该会让你走上正确的道路:

    <asp:ListView ID="lstOuterList" runat="server" DataKeyNames="ID, OtherColumn">
        <LayoutTemplate>
            <table width="100%">
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td><asp:LinkButton ID="LinkButton1" runat="server" Text="Expand" OnCommand="LinkButton1_Command" CommandArgument='<%#Container.DisplayItemIndex%>'></asp:LinkButton></td>
                <td><%#Eval("Value")%></td>
                <td><%#Eval("OtherValue")%></td>
                <td><%#Eval("OtherOtherValue")%></td>
            </tr>
            <asp:PlaceHolder ID="plcInnerList" runat="server">
                <asp:ListView ID="lstInnerList" runat="server" Width="100%">
                    <LayoutTemplate>
                        <tr>
                            <td colspan="4">                                
                                <div style="padding:20px;background-color:#fffeee;">
                                    <table width="100%">
                                        <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
                                    </table>
                                </div>
                            </td>                        
                        </tr>
                    </LayoutTemplate>   
                    <ItemTemplate> 
                        <tr>                       
                            <td><%#Eval("Value")%></td>
                            <td><%#Eval("OtherValue")%></td>
                            <td><%#Eval("OtherOtherValue")%></td>  
                        </tr>                         
                    </ItemTemplate>
                </asp:ListView>
            </asp:PlaceHolder>
        </ItemTemplate>        
    </asp:ListView>
    

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

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        //pass index of item in command argument
        var itemIndex = Convert.ToInt32(e.CommandArgument);      
    
        //find the pnlChildView control
        var innerPlaceHolder = lstOuterList.Items[itemIndex].FindControl("plcInnerList") as PlaceHolder;
        if (innerPlaceHolder != null)
        {
            innerPlaceHolder.Visible = !innerPlaceHolder.Visible;          
            if (innerPlaceholder.Visible)
            {
                var innerList = innerPlaceHolder.FindControl("lstInnerList") as ListView;
                if (innerList != null)
                {
                    //the id to retrieve data for the inner list
                    int keyValue = (int)lstOuterList.DataKeys[itemIndex]["ID"];
    
                    //bind the list using DataList1 data key value
                    innerList.DataSource = new DataTable("DataSource"); //your datasource
                    innerList.DataBind();
                }  
            }
        }
    }
    

    【讨论】:

    • 啊,我从未使用过 ListView,我将不得不试一试。谢谢!
    • 更新了我的示例以使用 ListView 而不是 DataListListView 更适合这种类型的功能。
    【解决方案2】:

    一种方法是:

    在主网格的rowcommand上:

    • 为网格创建 c# 代码将在 GridView grd = new GridView();

    • 像绑定任何其他网格一样绑定此实例

    • 从主网格当前行添加控件,应该类似于

      e.Cells[0].Controls.Add(grd);
      

    我现在这里没有 VS,但我想你可以理解,我一直使用这种方法

    【讨论】:

    • 所以这会创建一个没有任何数据源或格式的 Gridview?
    • 在您创建“grd”之后,您可以像任何其他网格一样绑定它(在添加到 .Controls 之前绑定它。),并访问它的属性,例如“Autogeneratecolumn”并添加自定义列...唯一的区别是它会在代码隐藏上完成......试试吧
    • 这很有趣。我会试试看的!
    猜你喜欢
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2011-12-31
    相关资源
    最近更新 更多