1
我使用這段代碼在gridview中刪除行。它應該是這樣的「,當它刪除一行時,以序列號5爲第5行,在第6行的序列字段應爲5.意味着在刪除一行後遞減序列號。」但是當我刪除第5行的第5行時,行號不會發生,第6行保持不變。自動遞減行序列號,同時刪除
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
For Each control As Control In e.Row.Cells(0).Controls
Dim DeleteButton As LinkButton = TryCast(control, LinkButton)
If DeleteButton IsNot Nothing AndAlso DeleteButton.Text = "Delete" Then
DeleteButton.OnClientClick = "return(confirm('Are you sure you want to delete this record?'))"
End If
Next
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Delete" Then
' get the categoryID of the clicked row
Dim Serial As Integer = Convert.ToInt32(e.CommandArgument)
' Delete the record
Dim mycommand As New SqlCommand("DELETE FROM target WHERE SlNo = '" & Serial & "'", con)
con.Open()
mycommand.ExecuteNonQuery()
con.Close()
bindphoto2()
Label1.Text = "File has been deleted succefully"
End If
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim Serial As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value)
Dim mycommand As New SqlCommand("DELETE FROM target WHERE SlNo = '" & Serial & "'", con)
con.Open()
mycommand.ExecuteNonQuery()
con.Close()
你想通過沒有任何間隙的序列號來實現什麼?保持這樣的序列號意味着更新所有以後的記錄以減少序列 - 取決於記錄集的大小,這可能是昂貴的操作。大多數情況下,計算出的序列號(例如使用排名函數)或隱含的(按有序集內的索引)就足夠了。 – VinayC
+1。儘管如此,它還是可以讓一些事情變得瑣碎 - 運行項目編號(發票,發票項目,同時編輯發票),但是你永遠不會使用它作爲ID(可能的令人討厭的副作用噸),只能作爲一個字段。 – TomTom