简体   繁体   English

将数据绑定到页脚中的嵌套网格

[英]Bind data to nested grid in footer

I have added programatically button to the footer in RowDataBound: 我以编程方式在RowDataBound的页脚中添加了按钮:

    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.CommandName = "ShowAll";
        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);
    }

now I want to handle showStaff_Click to bind data to nested gridview. 现在,我想处理showStaff_Click以将数据绑定到嵌套的gridview。 I wrote bwlow code in RowCommand but index is null: 我在RowCommand中编写了bwlow代码,但索引为null:

            if(e.CommandName == "ShowAll")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());                
                GridViewRow row = Staff.Rows[index];
                GridView StaffInfo = (GridView)Staff.Rows[index].FindControl("StaffInfo"); 
                int TeamID = 0;
                int cityID = 3699;
                StaffInfo.DataSource = GetStaff(cityID, TeamID);
                StaffInfo.DataBind();
            }

I will appreciate Your help. 感谢您的帮助。

EDIT: 编辑:

I have nade some changes: 我有一些变化:

    protected void Staff_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        //Checking for command name which command/button is pressed
        if (e.CommandName == "ShowDetails")
        {
            GridView Staff = (GridView)sender;

            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = Staff.Rows[index];
            GridView StaffInfo = (GridView)Staff.Rows[index].FindControl("StaffInfo");
            int TeamID = Convert.ToInt16(Staff.DataKeys[index].Values[0].ToString());
            int cityID = Convert.ToInt16(Staff.DataKeys[index].Values[1].ToString());
            StaffInfo.DataSource = GetStaff(cityID, TeamID);
            StaffInfo.DataBind();
            StaffInfo.Visible = true;
        }
        else if(e.CommandName == "ShowAll")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridView StaffInfo = (GridView)Staff.Rows[index].FindControl("StaffInfo");
            int TeamID = 0;
            int cityID = 3699;
            StaffInfo.DataSource = GetStaff(cityID, TeamID);
            StaffInfo.DataBind();
            StaffInfo.Visible = true;
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

<asp:GridView ID="Staff" runat="server" AutoGenerateColumns="false"  
    OnRowCommand="Staff_RowCommand" 
    DataKeyNames="TeamID,CityID" ShowFooter="True" HorizontalAlign="Center">
<Columns>
    <asp:TemplateField HeaderText=" Function">
        <ItemTemplate>
            <asp:Label Width="150px" ID="Function" ItemStyle-HorizontalAlign="Center" runat="server" Text='<%# Bind("Function") %>'></asp:Label>
            <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>
    <asp:TemplateField HeaderText=" Team">
        <ItemTemplate>
            <asp:Label Width="150px" ID="Team" ItemStyle-HorizontalAlign="Center" runat="server" Text='<%# Bind("Team") %>'></asp:Label> 
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Staff Count">
        <ItemTemplate>
            <asp:Button Width="40px" ID="StaffCount" ItemStyle-HorizontalAlign="Center" runat="server" Text='<%# Bind("StaffCount") %>' CommandName="ShowDetails" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" CausesValidation="True" UseSubmitBehavior="False" />                   
        </ItemTemplate>
            <FooterTemplate>
                <asp:Button id="TotalStaff" runat="server" Text="Button1" CommandName="ShowAll" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CausesValidation="True" UseSubmitBehavior="False" />
            </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField Visible ="false">
        <ItemTemplate>
            <asp:Label runat="server" Width="150px" DataField="TeamID" HeaderText="TeamID" ></asp:Label>
        </ItemTemplate>
    </asp:TemplateField> 
    <asp:TemplateField Visible ="false">
        <ItemTemplate>  
            <asp:Label runat="server" ItemStyle-Width="150px" DataField="CityID" HeaderText="CityID"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>    

When I try to click the ShowAll button ite returns that index is out of range. 当我尝试单击ShowAll按钮ite时,返回的索引超出范围。 I made Response.Write(intex.ToString()) and it returns -1. 我做了Response.Write(intex.ToString()),它返回-1。 The index number in case or row is returned correctly. 正确返回大小写或行的索引号。 The problem is with footer button. 问题出在页脚按钮上。 Could You please help me? 请你帮助我好吗?

You did not set CommandArgument when you crated your button. 创建按钮时未设置CommandArgument

Button showStaff = new Button();
showStaff.CommandName = "ShowAll";
showStaff.CommandArgument = e.Row.RowIndex.ToString();
showStaff.Text = e.Row.Cells[2].Text.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM