0

以下のように、ネストされたグリッドビューを作成しました。

<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 に行の定義が含まれていないというエラーが発生しました。

    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()); を表示しようとすると、エラーが発生しました:

インデックスが範囲外でした。負ではなく、コレクションのサイズより小さくなければなりません。パラメータ名:インデックス

保存された手順を確認しましたが、正常に動作します。何をすべきか?

4

1 に答える 1