Gridview的RowDeleting事件 [英] RowDeleting event of Gridview
本文介绍了Gridview的RowDeleting事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我紧急,
我将gridview作为gvclass,我的问题是我想从gridview中单击删除按钮时从gridview删除一条记录,gridview的 gvClass_RowDeleting事件执行两次,一次,我正在发布代码,请参见该代码波纹管和
请给我解决方法.
Hi,
Please help me urgent,
i have gridview as gvclass my problem is i want to delete one record from gridview when i click on delete button from gridview, gvClass_RowDeleting event of gridview executing twice insted of once, i am posting code please see that code bellow and
please give me solution.
Protected Sub gvClass_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvClass.RowDeleting
Try
Dim chk As CheckBox = DirectCast(gvClass.Rows(e.RowIndex).FindControl("chk"), CheckBox)
If chk.Checked Then
Dim admno As Integer = Convert.ToInt32(gvClass.DataKeys(e.RowIndex).Values("AdmNo").ToString())
'Dim stuName As String = gvClass.DataKeys(e.RowIndex).Values("AdmNo").ToString()
cn.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
cn.Open()
Dim cmd As SqlCommand = New SqlCommand("delete from AdmTable where AdmNo=" & admno, cn)
Dim result As Integer = cmd.ExecuteNonQuery()
cn.Close()
If result = 1 Then
BindDataGrid1()
lblresult.ForeColor = Color.Green
lblresult.Text = "Admission Number" & admno & " details deleted successfully"
End If
Else
ClientScript.RegisterStartupScript(Me.GetType, "alert", "javascript:alert('Please check the CkeckBox');", True)
End If
Catch ex As Exception
ClientScript.RegisterStartupScript(Me.GetType, "alert", "javascript:alert('" & ex.Message & "');", True)
End Try
End Sub
推荐答案
如果您的数据网格是数据绑定的,您是否需要在代码中使用SQL从数据库中删除项目?我认为一旦数据绑定,它就会自动处理,也许可以解释为什么它执行两次?
当我使用datagridviews时,我更喜欢在内存中保存一个表,该表可以作为数据源绑定到datagridview.
要删除内容,方法很简单:
datagridview.datasource =无
datatable.rows(idx).delete
datatable.acceptchanges
datagridview.datasource = datatable
if your datagrid is databound do you need to have the code inside deleting items from the database w/ SQL? I would think once it''s databound then that''d be taken care of automatically, perhaps explaining why it executes twice?
When I work with datagridviews I prefer holding a table in memory that I can bind as a datasource to the datagridview.
To Delete stuff it''s simple this way:
datagridview.datasource = nothing
datatable.rows(idx).delete
datatable.acceptchanges
datagridview.datasource = datatable
这篇关于Gridview的RowDeleting事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文