Reputation:
I have written like below lines of code
in aspx file
<asp:TemplateField HeaderText="Is Active">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" CssClass="colorlnkbtnedit" runat="server" ToolTip="Edit" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "ProductDocument") %>'
CommandName="EditIsActive"><i class=" icon-pencil"></i> </asp:LinkButton>
<asp:Panel ID="pnlIsActEdit" runat="server" Visible="false">
...
...
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
in .cs file
protected void gvProductView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
...
...
}
else if (e.CommandName == "EditIsActive")
{
int rowID = Convert.ToInt32(e.CommandArgument);
GridViewRow r = gvProductView.Rows[rowID];
Panel p = (Panel)r.FindControl("pnlIsActEdit");
p.Visible = false;
}
}
It's not working !!! It throws "Index was out of range. Must be non-negative and less than the size of the collection"
error message at GridViewRow r = gvProductView.Rows[rowID];
line of code.
Upvotes: 1
Views: 1299
Reputation: 460138
You exptect the index of the row in the CommandArgument
but actually you are initializing it with the ProductDocument
of the underyling datasource here:
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ProductDocument") %>'
So this causes the exception.
Instead use:
CommandArgument='<%# Container.DataItemIndex %>'
or
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
Upvotes: 1
Reputation: 38683
Simply you have 10 rows only, but if your rowID
is greater than 11. then you got his error.
Please verify your row count and rowID
value to solve this issue.
I think you need to get current selected row. if yes, then please see this MSDN Document
Upvotes: 0