ASP.NET中的Gridview删除按钮

时间:2011-02-11 13:18:06

标签: c# .net asp.net gridview delete-row

我刚问了一个关于这个问题的问题; All Row has a Delete Button in Gridview

我有一个简单的表AVUKAT

列 - > HESAPMUSTERIAVUKAT

我在Gridview中显示数据。

我激活AutoGenerateDeleteButton

enter image description here

但是当我点击一行的删除按钮时,我收到这样的错误。

Server Error in '/' Application.
Deleting is not supported by data source 'GridviewDataSource' unless DeleteCommand is specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Deleting is not supported by data source 'GridviewDataSource' unless DeleteCommand is specified.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

单击删除按钮时会激活哪个功能?

这个函数的代码应该是什么?

3 个答案:

答案 0 :(得分:2)

请查看此MSDN文章:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.deletecommand.aspx#Y725

它描述了设置SQLDataSource以从数据库中删除数据。

<强>更新

您的SQLDataSource缺少“删除参数”(MSDN Link)。

更新您的SQLDataSource以包含DeleteParameters,如下所示(您需要更新ControlParameters的ConnectionString和ControlID等内容):

<asp:SqlDataSource
   id="SqlDataSource1"
   runat="server"
   ConnectionString="myConnectionString"
   SelectCommand="SELECT * FROM [AVUKAT] ORDER BY [MUSTERI]" 
   DeleteCommand="DELETE FROM [AVUKAT] WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP">
   <DeleteParameters>
      <asp:ControlParameter Name="MUSTERI" ControlId="DropDownListID" PropertyName="SelectedValue" />
      <asp:ControlParameter Name="AVUKAT" ControlId="DropDownListID" PropertyName="SelectedValue" />
      <asp:ControlParameter Name="HESAP" ControlId="DropDownListID" PropertyName="SelectedValue" />
   </DeleteParameters>
</asp:SqlDataSource>

答案 1 :(得分:1)

除此之外,请记住在您的表中使用主键,您将在其中运行此Delete命令。在没有主键的情况下,如果多个行具有相同的值,则删除命令将引发异常,因为生成的查询可能不明确  (事实上​​,除非你的表有一个主键,否则你将无法附加内置的DELETE命令。)

答案 2 :(得分:0)

您需要按this tutorial

中所述实现RowDeleting事件