【问题标题】:Expand nested gridview展开嵌套的网格视图
【发布时间】:2013-06-18 06:16:37
【问题描述】:

我创建了如下嵌套的gridview:

<asp:GridView ID="Staff" runat="server" AutoGenerateColumns="false"  OnRowCommand="Staff_RowCommand" OnRowDataBound="Staff_RowDataBound">
<Columns>
    <asp:BoundField ItemStyle-Width="200px" DataField="Function" HeaderText=" Function" ItemStyle-HorizontalAlign="Center" />
    <asp:BoundField ItemStyle-Width="200px" DataField="Team" HaeaderText=" Team" ItemStyle-HorizontalAlign="Center" />
    <asp:BoundField ItemStyle-Width="200px" DataField="StaffCount" HeaderText="Staff Count" ItemStyle-HorizontalAlign="Center" />

    <asp:BoundField ItemStyle-Width="150px" DataField="FunctionID" HeaderText="FunctionID" Visible="true" />     
    <asp:BoundField ItemStyle-Width="150px" DataField="TeamID" HeaderText="TeamID" Visible="true" />
    <asp:BoundField ItemStyle-Width="150px" DataField="CityID" HeaderText="CityID" Visible="true" />
    <asp:CommandField ShowSelectButton="True" SelectText="Show Details"/>
    <asp:TemplateField> 
        <ItemTemplate>
            <asp:GridView ID="StaffInfo" AutoGenerateColumns="false" runat="server"> 
                <Columns>
                    <asp:BoundField ItemStyle-Width="150px" DataField="FirstName" HeaderText="First Name"  />
                    <asp:BoundField ItemStyle-Width="150px" DataField="LastName" HeaderText="Last Name" />
                    <asp:BoundField ItemStyle-Width="150px" DataField="SOEID" HeaderText="SOEID" />
                </Columns>
            </asp:GridView> 
        </ItemTemplate>
    </asp:TemplateField> 
</Columns>

protected void Staff_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        GridView StaffInfo = e.Row.FindControl("StaffInfo") as GridView;
        int functionid = Convert.ToInt32(e.Row.Cells[3].Text);
        int teamid = Convert.ToInt32(e.Row.Cells[4].Text);
        int cityid = Convert.ToInt32(e.Row.Cells[5].Text);
        StaffInfo.DataSource = GetStaff(cityid, functionid, teamid);
        StaffInfo.DataBind();

        totalStaff += Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "StaffCount"));
        Button showStaff = new Button();
        showStaff.ID = "this1";
        showStaff.Text = e.Row.Cells[2].Text.ToString();
        e.Row.Cells[2].Controls.Add(showStaff);
        showStaff.Click += new EventHandler(showStaff_Click);

    }

    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[1].Text = "Total: ";
        e.Row.Cells[2].Text = totalStaff.ToString();
        Button showStaff = new Button();
        showStaff.ID = "this2";
        showStaff.Text = e.Row.Cells[2].Text.ToString();
        e.Row.Cells[2].Controls.Add(showStaff);
        e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
        showStaff.Click += new EventHandler(showStaff_Click);
    }
}

我想在单击 showStaff 按钮时展开嵌套的网格视图。 我还想将参数传递给单击按钮后启动的存储过程,但是在我使 id 字段不可见后它不起作用:我收到错误,参数不正确(当我设置字段可见时没有错误)。

编辑:

我在下面添加了事件处理程序,但我收到 GridViewCommandArgs 不包含 Row 定义的错误:

    protected void Staff_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        //Checking for command name which command/button is pressed
        if (e.CommandName == "ShowDetails")
        {
            GridView StaffInfo = e.Row.FindControl("StaffInfo") as GridView;
            int functionid = Convert.ToInt32(e.Row.Cells[3].Text);
            int teamid = Convert.ToInt32(e.Row.Cells[4].Text);
            int cityid = Convert.ToInt32(e.Row.Cells[5].Text);
            StaffInfo.DataSource = GetStaff(cityid, functionid, teamid);
            StaffInfo.DataBind();
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

我也改了asp部分:

<asp:ButtonField ItemStyle-Width="200px" ButtonType="button" DataTextField="StaffCount" CommandName="ShowDetails" HeaderText="Staff Count"  ItemStyle-HorizontalAlign="Center" />

EDIT2:

我写在下面的事件处理程序:

protected void Staff_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        //Checking for command name which command/button is pressed
        if (e.CommandName == "ShowDetails") 
        {
            int index = Convert.ToInt32(e.CommandArgument.ToString());
            GridViewRow row = Staff.Rows[index];
            GridView StaffInfo = (GridView)Staff.Rows[index].FindControl("StaffInfo");
            int cityID = Convert.ToInt16(row.Cells[5].Text.ToString());
            int TeamID = Convert.ToInt16(row.Cells[4].Text.ToString());

            StaffInfo.DataSource = GetStaff(cityID, TeamID);
            Staff.DataBind();

            Response.Write(StaffInfo.Rows[0].ToString());

        }
    }
    catch(Exception ex) 
    {
        Response.Write(ex.Message);
    }
}

但是当我尝试显示 Response.Write(StaffInfo.Rows[0].ToString());我得到了错误:

索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

我检查了存储的程序,它工作正常。有什么想法应该怎么做?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你正在动态地做很多我认为你不需要的事情。

    您已经拥有显示详细信息 CommandRow。如果您处理 GridView 的 RowCommand 事件,您可以捕获对该按钮的单击,并可以访问事件发生在哪一行。

    在该单击事件中,您可以调用存储过程、FindControl 嵌套的 GridView,并将存储过程结果绑定到嵌套的 GV。

    我认为这会更直接。除非有特定的原因你在做动态的东西。

    例子:

    在您的 CommandField 中,设置属性 CommandText = "ShowDetailsCommand"。然后,为 Staff.RowCommand 设置事件处理程序。

    在 Staff_OnRowCommand 方法中,添加一个条件:

    if(e.CommandText=="ShowDetailsCommand")
    {
        GridView StaffInfo = e.Row.FindControl("StaffInfo") as GridView;
        int functionid = Convert.ToInt32(e.Row.Cells[3].Text);
        int teamid = Convert.ToInt32(e.Row.Cells[4].Text);
        int cityid = Convert.ToInt32(e.Row.Cells[5].Text);
        StaffInfo.DataSource = GetStaff(cityid, functionid, teamid);
        StaffInfo.DataBind();
    }
    

    【讨论】:

    • 用示例编辑。与您已有的没有太大区别。
    • 编辑了第一篇文章。加里森,如果你能帮我解决这个问题,我将不胜感激。
    • 抱歉——发现默认的 e.CommandArgument 将是行索引。查看此页面上的示例以获取当前行:msdn.microsoft.com/en-us/library/…
    • 对不起,Garrison,但我仍然不知道它应该是什么样子。
    • 感谢 Garrison 您帮了我很多,但我仍然无法填充内部网格视图。请在第一篇文章中查看 EDIT 2。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多