2012-11-05 42 views
2

刪除記錄我有一個GridView控件與下面的標記:錯誤從GridView中

<asp:GridView ID="gvGroups" runat="server" Width="100%" AutoGenerateColumns="False" 
      ShowFooter="True" BorderColor="White" BorderStyle="Ridge" CellSpacing="1" BorderWidth="2px" 
      BackColor="White" CellPadding="3" GridLines="None" Font-Names="Tahoma" Font-Size="11px" 
      DataKeyNames="GroupId" OnRowDeleting="gvGroups_RowDeleting"> 
      <FooterStyle BackColor="#C6C3C6" ForeColor="Black" /> 
      <RowStyle BackColor="#DEDFDE" ForeColor="Black" /> 
      <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" /> 
      <Columns> 
       <asp:TemplateField HeaderText="Row"> 
        <ItemTemplate> 
         <asp:Literal ID="litRowNumberNormal" runat="server"></asp:Literal> 
        </ItemTemplate> 
        <FooterTemplate> 
         <asp:Literal ID="litRowNumberFooter" runat="server"></asp:Literal> 
        </FooterTemplate> 
        <ItemStyle HorizontalAlign="Center" /> 
        <FooterStyle HorizontalAlign="Center" /> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Title"> 
        <ItemTemplate> 
         <%#Eval("Title")%> 
        </ItemTemplate> 
        <FooterTemplate> 
         <asp:TextBox ID="txtAddTitle" runat="Server" BorderStyle="Solid" BorderWidth="1px" 
          Font-Names="Tahoma" Font-Size="11px" BorderColor="Black" /> 
        </FooterTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtEditTitle" Text='<%# Bind("Title") %>' runat="server" BorderStyle="Solid" 
          BorderWidth="1px" Font-Names="Tahoma" Font-Size="11px" BorderColor="Black" /> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:CommandField ShowEditButton="True" ButtonType="Button" UpdateText="Save" CancelText="Cancel" 
        EditText="Edit" HeaderText="Edit"> 
        <FooterStyle BackColor="#669900" HorizontalAlign="Center" /> 
        <HeaderStyle BackColor="#5A49A7" HorizontalAlign="Center" /> 
        <ItemStyle BackColor="#FFC080" HorizontalAlign="Center" /> 
       </asp:CommandField> 
       <asp:TemplateField HeaderText="Delete"> 
        <FooterTemplate> 
         <asp:Button CommandName="Delete" Text="Delete" ID="btnRemove" runat="server" BorderStyle="Solid" 
          BorderWidth="1px" BackColor="#FFC080" Font-Names="Tahoma" Font-Size="11px" /> 
        </FooterTemplate> 
        <ItemTemplate> 
         <asp:CheckBox ID="ChkRemove" runat="server"></asp:CheckBox> 
        </ItemTemplate> 
        <ItemStyle BackColor="LightCoral" HorizontalAlign="Center" /> 
        <HeaderStyle BackColor="#5A49A7" HorizontalAlign="Center" /> 
        <FooterStyle BackColor="#669900" HorizontalAlign="Center" /> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

此網格模型是Group類的List。 Group類如下:

public class Group 
{ 
public int GroupId {get; set; } 
public string Title {get; set; } 
} 

的GroupId是我的表的主鍵。 當我按下刪除按鈕,我得到以下錯誤:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

我RowDeleting事件處理程序代碼:

protected void gvGroups_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    CheckBox chkRemove; 

    List<int> ids = new List<int>(); 


    foreach (GridViewRow gvRow in gvGroups.Rows) 
    { 
     chkRemove = (CheckBox)gvRow.FindControl("ChkRemove"); 
     if (chkRemove.Checked) 
     { 
      ids.Add(Int32.Parse(gvGroups.DataKeys[gvRow.RowIndex].Value.ToString())); 
     } 
    } 

    if (ids.Any()) 
    { 
     GroupService.DeleteGroupById(ids); 
    } 

    this.BindGroups(); 
} 
+1

'RowDeleting'處理程序用於刪除* current *行。您需要在「GridView」外部添加「按鈕」並編寫代碼以刪除選定(選中)的行。 – adatapost

+0

編號在本文中:http://www.codeproject.com/Articles/20734/Customary-Functions-of-GridView-in-ASP-NET-3-5作者在GridView中添加了刪除按鈕。 –

+0

哪行代碼拋出異常? –

回答

1

我們可以做的另一項工作是將CommandName屬性的「刪除」按鈕更改爲除「刪除」之外的任何其他內容,並在RowCommand事件中處理它, 「刪除「命令是用於觸發GridView co的RowDeleting事件的默認CommandName ntrol。

0

代碼看起來正確的,但選擇的事件不是。作爲@AVD提到RowDeleting事件是per row當每row都有自己Delete button

Occurs when a row's Delete button is clicked, but before the GridView control deletes the row

所有你需要的是增加一個btnRemove_Click事件,並把你的代碼中有。

+0

感謝mohsen。但這篇文章的作者如何http://www.codeproject.com/Articles/20734/Customary-Functions-of- GridView-in-ASP-NET-3-5這樣做嗎? –

+0

我應該自己檢查一下代碼,你可以在這裏使用其中一個答案,直到你弄清楚什麼是錯誤的 –

0

如果你想要做的多刪除添加一個按鈕,稱爲多刪除GridView的外面和處理onclick事件,在事件處理程序刪除選擇喜歡的項目數低於

public void DeleteEverything(object sender, EventArgs e) 
    { 
     // this function is to delete the selected items based on the checkbox 
     CheckBox chkAll = (CheckBox)GridView1.HeaderRow.FindControl("SelectAllCheck"); 
     // to get the Checkbox status in the header rows 
     if (chkAll.Checked == true) 
     { 
      int i = 0; 
      foreach (GridViewRow gvRow in GridView1.Rows)//to get all rows in that particular page 
      { 
       string Delete = Convert.ToString(GridView1.Rows[i].Cells[3].Text); 
       //Cells[3] is the column to get one by one rows cells[3] columns where you should keep your primary keys and in visible state 
       Bal_add.Delete(Delete); 
       i++; 
      } 
      Response.Redirect("Information.aspx"); 
     } 
     else 
     { 
      int j=0; 
      foreach (GridViewRow gvRow in GridView1.Rows) 
      { 
       CheckBox chkSel = (CheckBox)gvRow.FindControl("SelectCheck"); 
       if (chkSel.Checked == true) 
       { 
        string Delete = Convert.ToString(GridView1.Rows[j].Cells[3].Text); 
        //Cells[3] is the column to get one by one rows cells[3] columns where you should keep your primary keys and in visible state 
        Delete(Delete); 

       } 
       j++; 

      } 
      Response.Redirect("Information.aspx"); 
     } 

    } 

public void Delete(string UserEmail) 
     { 
      obj_add = new add(); 
      string QueryString; 
      QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["Admin_raghuConnectionString1"].ToString(); 

      obj_SqlConnection = new SqlConnection(QueryString); 

      obj_SqlCommand = new SqlCommand("usp_DeleteDataProcedure"); 
      obj_SqlCommand.CommandType = CommandType.StoredProcedure; 
      obj_SqlConnection.Open(); 

      obj_SqlCommand.Parameters.AddWithValue("@UserEmail", UserEmail);//here @UserName is the variable that we declare in the stored procedure 
      obj_SqlCommand.Connection = obj_SqlConnection; 
      SqlDataReader obj_result = null; 

      obj_SqlCommand.CommandText = "usp_DeleteDataProcedure"; 
      obj_result = obj_SqlCommand.ExecuteReader(); 
      obj_SqlConnection.Close(); 

     } 

你可以刪除根據您的主鍵在存儲過程希望這會有所幫助:D