我试图传递行的索引...并且效果很好......但我的gridview具有分页启用功能,当我在第二页中使用索引时,例如,我得到了超出范围错误..但我收到的索引是正确的。例如..我在第一页有4行,在第二页有4行。如果我点击第二页的第一项,我得到索引5 ......它是正确的。我有8个itens ...为什么索引5超出范围?
我的代码是:
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
BorderStyle="None" BorderWidth="0px" CellSpacing="1" Width="100%"
GridLines="Vertical" AllowPaging="True" onrowcommand="GridView1_RowCommand"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound" PageSize="5" HorizontalAlign=Left
>
<PagerStyle HorizontalAlign="Center" />
<RowStyle CssClass="tabela_texto2" HorizontalAlign="Center"
VerticalAlign="Middle" />
<AlternatingRowStyle CssClass="tabela_texto1" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Status" CommandName="atender" CommandArgument='<%# Container.DataItemIndex %>' ID="atender" runat="server" OnClientClick="return confirm('Tem certeza que deseja aterar os status?');" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Ver no mapa" CommandName="ver" CommandArgument='<%# Container.DataItemIndex %>' ID="ver" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Excluir" CommandName="excluir" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' ID="excluir" runat="server" OnClientClick="return confirm('Tem certeza que deseja aterar os status?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
并且获得错误的行是:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandname = e.CommandName;
int index = int.Parse(e.CommandArgument.ToString());
if (commandname.Equals("atender"))
{
string id = GridView1.Rows[index].Cells[3].Text //this line is getting error.
}
}
答案 0 :(得分:3)
这可能是一种解决方法:
int index = int.Parse(e.CommandArgument.ToString()) % GridView1.PageSize