展开嵌套的gridview

时间:2013-06-17 16:50:14

标签: c# asp.net gridview

我已经创建了嵌套的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按钮时展开嵌套的gridview。 我也想将参数传递给我点击按钮后启动的存储过程,但是在我使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());我收到了错误:

  

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

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

1 个答案:

答案 0 :(得分:1)

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

您已经拥有Show Details CommandRow。如果您处理GridView的RowCommand事件,则可以捕获该按钮的单击,并可以访问事件发生的行。

在click事件中,您可以调用存储过程,FindControl嵌套GridView,并将sproc结果绑定到嵌套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();
}