c# - 获取页脚的 RowIndex

标签 c# asp.net gridview footer

我有如下代码。我的问题是我无法获取页脚的行索引。使用代码,我得到了页脚的 rowindex = -1。我试图在网上找到解决方案,但没有找到任何东西。感谢您帮助我。先感谢您。

  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> 

最佳答案

您可以通过以下代码获取页脚行-

gridview1.FooterRow   // Use name of your gridview

检查它是否有任何索引属性,以便您可以获得页脚行的索引。

可能是 gridview1.FooterRow.RowIndex//未测试

关于c# - 获取页脚的 RowIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189885/

相关文章:

c# - 无法添加引用 .NET

asp.net - Blazor WeatherForecast 示例如何工作?

asp.net - 引发 CacheItemRemovedCallback 时,HttpContext.Current 为 null

php - 如何在 YII 中更改 GridView 列的大小?

c# - 如何去除 Crystal 报表中的小数位

c# - Mongo C# 驱动程序使用 elemMatch 时如何从投影中排除元素?

JQuery 数据表和 ASP.NET UpdatePanel

android - gridview的锁定滚动

c# - 如何检查是否从 GridView 中选择了任何行?

c# - 循环遍历类中的关联列表?