---
title: "Index was out of range in grid row command"  
description: "Index was out of range in grid row command"  
author: "Anonymous User"  
published: 2014-09-01  
updated: 2014-09-01  
canonical: https://www.mindstick.com/forum/2217/index-was-out-of-range-in-grid-row-command  
category: "asp.net"  
tags: ["asp.net", "grid row command", ".net", "c#", "Index was out of range in grid row command", "Index was out of range", "open source"]  
reading_time: 1 minute  

---

# Index was out of range in grid row command

I'd like to reach [gridView](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) a cell's value.But it gives the error is 'Index was out of range. Must be non-negative and less than the size of the collection. grid row [command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)'

Can somebody help me?

<asp:GridView ID="grdList" runat="[server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)" AutoGenerateColumns="False" GridLines="None"

PageSize="5" AllowPaging="True" OnPageIndexChanging="Grd_Allergys_PageIndexChanging"

OnRowCommand="grdList_RowCommand" OnRowDeleting="grdList_RowDeleting" OnRowEditing="grdList_RowEditing">

<Columns>

<asp:[BoundField](https://www.mindstick.com/forum/12737/get-the-value-of-a-boundfield-from-a-detailsview) DataField="LangId" HeaderText="LangId" />

<asp:BoundField DataField="LangName" HeaderText="LangName" />

<asp:TemplateField HeaderText="Edit">

<[ItemTemplate](https://www.mindstick.com/forum/2057/access-textbox-in-itemtemplate)>

<asp:[ImageButton](https://www.mindstick.com/articles/12753/imagebutton-in-android) ID="imgBtnEdit" runat="server" CommandName="Edit" CommandArgument='<%#Eval("LangId") %>'

ImageUrl="~/[Image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character)/edit.jpg" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="[Delete](https://www.mindstick.com/forum/33620/how-to-delete-record-from-list-in-mvc-using-ajax)">

<ItemTemplate>

<asp:ImageButton ID="imgBtnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("LangId")%>'

ImageUrl="~/Image/delete_icon.gif" OnClientClick="return [confirm](https://answers.mindstick.com/qa/115761/facebook-account-locked-with-confirm-your-identity-loop)('Do you want to continue ?')" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<PagerSettings Mode="Numeric" />

</asp:GridView>

protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)

{

DBDataContext dc = new DBDataContext();

if (e.CommandName == "Edit")

{

int rowIndex = int.Parse(e.CommandArgument.ToString());

[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) LangName = grdList.DataKeys[rowIndex]["LangName"].ToString();

}

}

## Replies

### Reply by Sumit Kesarwani

Hi Jeet, \

You are assigning LangId and assuming it as row index in command argument. If id is greater then number of rows then you should get out of range index

Change

CommandArgument='<%#Eval("LangId")%>'

To

CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'


---

Original Source: https://www.mindstick.com/forum/2217/index-was-out-of-range-in-grid-row-command

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
