2016-11-18 107 views
0

Here is the image of the UI我在這裏在asp.net初學者,我希望能問,如果你們知道如何解決我的問題。我有一個刪除按鈕,刪除我的datagridview中選中的複選框。每次運行代碼時,它都會給出在int id = Convert.ToInt32(brandgrid.DataKeys[row.RowIndex].Values[0]);代碼中超出範圍錯誤的參數(索引超出範圍)?ASP.NET GridView的DataKeys錯誤 - 參數超出範圍的指數超出範圍

  foreach (GridViewRow row in brandgrid.Rows) 
      { 

       int id = Convert.ToInt32(brandgrid.DataKeys[row.RowIndex].Values[0]); 

       CheckBox chkdel = (CheckBox)row.FindControl("chkDel"); 
       if(chkdel.Checked) 
        { 
         //int id = Convert.ToInt32(row.Cells[0].Text); 
         SqlCommand cmd = new SqlCommand(); 
         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.CommandText = "usp_Brand_Delete"; 
         cmd.Parameters.AddWithValue("@login", current); 
         cmd.Parameters.AddWithValue("@id", id); 
         cmd.Connection = sqlCon; 

         sqlCon.Open(); 
         cmd.ExecuteNonQuery(); 
         sqlCon.Close(); 
        } 
      } 

     } 
+0

你可以發佈你的'GridView'在aspx頁面? – Aruna

+0

Jetlag

+0

貴GridView的第一填補?你是否偶然檢查過沒有列。你調試了嗎?這段代碼執行前的行數和列數是多少? –

回答

0

好了,從下面的GridView,它的確認,您沒有設置DataKeyNames屬性在GridView等,其如你上面所說的引發錯誤。

<asp:GridView ID="brandgrid" runat="server" GridLines="None" AllowPaging="True" CssClass="mGrid" ShowHeaderWhenEmpty="True" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" DataSourceID="SqlDataSource1"> 
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
    <PagerStyle CssClass="pgr"> 
    </PagerStyle> 
</asp:GridView> 

從MSDN,

當DataKeyNames屬性被設置時,GridView控制 自動創建用於控制每一行DataKey對象。 的DataKey對象包含在DataKeyNames屬性中指定的一個或多個字段 的值。然後將DataKey對象添加到控件的DataKeys集合中,即 。使用DataKeys屬性 檢索特定數據行的DataKey對象在GridView 控制。

要訪問此屬性,您必須在GridView中設置DataKeyNames="<ID column name>"

我在GridView中添加了DataKeyNames="id"屬性,假設列名爲ID,但您必須檢查並更改此屬性。

<asp:GridView ID="brandgrid" runat="server" DataKeyNames="id" GridLines="None" AllowPaging="True" CssClass="mGrid" ShowHeaderWhenEmpty="True" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" DataSourceID="SqlDataSource1"> 
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
    <PagerStyle CssClass="pgr"> 
    </PagerStyle> 
</asp:GridView> 

也改變如下代碼,

foreach (GridViewRow row in brandgrid.Rows) 
{ 
    if(row.RowType == DataControlRowType.DataRow) 
    { 
      var dataKey = brandgrid.DataKeys[row.RowIndex].Value; 
      if(dataKey != null) 
      { 
       int id = (int)dataKey; 

       CheckBox chkdel = (CheckBox)row.FindControl("chkDel"); 
       if(chkdel.Checked) 
        { 
         //int id = Convert.ToInt32(row.Cells[0].Text); 
         SqlCommand cmd = new SqlCommand(); 
         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.CommandText = "usp_Brand_Delete"; 
         cmd.Parameters.AddWithValue("@login", current); 
         cmd.Parameters.AddWithValue("@id", id); 
         cmd.Connection = sqlCon; 

         sqlCon.Open(); 
         cmd.ExecuteNonQuery(); 
         sqlCon.Close(); 
        } 
      } 

     } 
    } 
+0

我忘了編輯,我沒有我的數據源,我手動連接它通過代碼 – Jetlag

+0

數據源是好的,如果你在代碼來處理它。正如我告訴 – Aruna

+0

添加'DataKeyNames'我得到這個:DataBinding:'System.Data.DataRowView'不包含名稱爲'id'的屬性。 – Jetlag