2011-10-19 26 views
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() 
+2

你想通過沒有任何間隙的序列號來實現什麼?保持這樣的序列號意味着更新所有以後的記錄以減少序列 - 取決於記錄集的大小,這可能是昂貴的操作。大多數情況下,計算出的序列號(例如使用排名函數)或隱含的(按有序集內的索引)就足夠了。 – VinayC

+0

+1。儘管如此,它還是可以讓一些事情變得瑣碎 - 運行項目編號(發票,發票項目,同時編輯發票),但是你永遠不會使用它作爲ID(可能的令人討厭的副作用噸),只能作爲一個字段。 – TomTom

回答

0

這可能有助於:)

昏暗strRowNumber作爲字符串=(e.RowIndex + 1)的ToString()

While strRowNumber.Length < dgvFilter.RowCount.ToString().Length 
     strRowNumber = "0" & strRowNumber 
    End While 

e.Graphics.DrawString (strRowNumber,Me.Font,b,e.RowBounds.Location.X + 15,e.RowBounds.Location.Y +((e.RowBounds.Height - size.Height)/ 2)) 'Write this code on Row PostPaint事件 小心