2013-04-01 17 views
1

我想到達gridView的單元格的值。但它給出的錯誤是'索引超出範圍。必須是非負數且小於集合的大小。網格行命令'索引超出了網格行命令的範圍

有人可以幫助我嗎?

<asp:GridView ID="grdList" runat="server" AutoGenerateColumns="False" GridLines="None" 
      PageSize="5" AllowPaging="True" OnPageIndexChanging="Grd_Allergys_PageIndexChanging" 
      OnRowCommand="grdList_RowCommand" OnRowDeleting="grdList_RowDeleting" OnRowEditing="grdList_RowEditing"> 
      <Columns> 
       <asp:BoundField DataField="LangId" HeaderText="LangId" /> 
       <asp:BoundField DataField="LangName" HeaderText="LangName" /> 
       <asp:TemplateField HeaderText="Edit"> 
        <ItemTemplate> 
         <asp:ImageButton ID="imgBtnEdit" runat="server" CommandName="Edit" CommandArgument='<%#Eval("LangId") %>' 
          ImageUrl="~/Image/edit.jpg" /> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Delete"> 
        <ItemTemplate> 
         <asp:ImageButton ID="imgBtnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("LangId")%>' 
          ImageUrl="~/Image/delete_icon.gif" OnClientClick="return confirm('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 LangName = grdList.DataKeys[rowIndex]["LangName"].ToString();        
     }      
} 

回答

1

要分配LangId,並假設它在命令參數index行。如果ID是大於行數,那麼你應該走出更改

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

的範圍index

CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' 
+0

它不工作,仍然給出了同樣的錯誤。 – baros

+0

你得到什麼錯誤? – Adil

+0

指數超出範圍 – baros

0

還沒有得到datakeys成立。將'DataKeys ='LangName''添加到你的gridview定義中。

0

commandargument = <%# Container.DisplayIndex %>

0

我有同樣的問題之前,這是我找到了解決辦法。

RowCommand的第一行必須是:

if (e.CommandName == "Page") return;