2015-09-10 54 views
0

我想刪除網格視圖中的行,這是我在rowcommand中的後端代碼。 我已經創建了一個OnRowDeleting,但函數中沒有任何內容。在gridview中刪除一行獲取錯誤

protected void grid2_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      switch (e.CommandName) // check the incoming command name 
      { 
case "remove": 
        if (grid2.Rows.Count > 1) 
        { 
         int rowIndex = Convert.ToInt32(e.CommandArgument); 
         //GridViewRow row = (GridViewRow)grid2.Rows[rowIndex]; 
         grid2.DeleteRow(rowIndex); //error pointing to this 
         grid2.DataBind(); 
        } 
        else 
        { 
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", " 
<script>alert('Last row');</script>"); 
         } 
         break; 
    } 

我的前端代碼

<ItemTemplate> 
<asp:Button ID="BtnDelete" runat="server" Text="Delete" 
CssClass="btn btn-primary" CommandName="remove" CommandArgument='<%# Container.DataItemIndex %>'/> 
</ItemTemplate> 

我得到這個錯誤,當我點擊刪除按鈕

'System.NotSupportedException' 類型的異常出現在 System.Web.dll中,但未在用戶代碼中處理

附加信息:刪除不受數據源支持除非指定了DeleteCommand,否則'SqlDataSource2'''。

數據源

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr_epsi %>" 
          ProviderName="<%$ ConnectionStrings:ConnStr_epsi.ProviderName %>" SelectCommand=""></asp:SqlDataSource> 

我網

<grd:MultiSelectGridView ID="grid2" runat="server" Width="500px" 
         CssClass="paging_gridview" AllowPaging="True" 
         AutoGenerateColumns ="false" PageSize="10" PagerType="Custom" 
         ShowFooter="true" OnRowDeleting="grid2_RowDeleting" 
         MultiSelectDataKeyName="Urid,Name" ShowHeaderWhenEmpty="true" 
         MultiSelectColumnIndex="0" EnableMultiSelect="false" GridLines="None" BorderStyle="None" OnRowCommand="grid2_RowCommand" 
         > 
+0

plz用數據源更新前端代碼 – Chandru

+0

谷歌會給你第一個搜索結果本身的解決方案,你用過它。 – niksofteng

+0

@Chandru嗨,即時更新 – Jan

回答

0

你必須把的CommandName爲 「Delete」 insted的的 「刪除」 例如:

<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn> 
+0

相同,如果我把刪除 – Jan

+0

也改變你的功能 –

+0

改變,什麼都沒有發生 – Jan