0

我创建了嵌套的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" />

编辑2:

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

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()); 我收到错误:

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

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

4

1 回答 1

1

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

您已经有了 Show Details 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();
}
于 2013-06-17T16:58:58.973 回答