展开嵌套网格视图

本文关键字:视图 网格 嵌套 | 更新日期: 2023-09-27 18:18:41

我已经创建了嵌套的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不包含行定义:

    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());I got error:

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

我检查了存储过程,它工作正常。有什么好主意吗?

展开嵌套网格视图

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

您已经拥有Show Details命令。如果您处理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();
}