下面的過程允許我通過選中我的數據網格中的複選框來一次刪除多條記錄。該程序是在ASP.net上編寫的,但現在我在VB.net上使用了一個winform。從sql綁定的Datagrid中刪除多條記錄
我有一個列名爲「刪除」的複選框所在的數據網格。用戶將檢查它想要刪除的記錄,並刪除那些記錄。我使用「票號」列值作爲查詢的參數。
我的問題是,既然是爲ASP.Net寫的,我無法找到如何在WinForm VB.net相當於該行:
Dim chkDelete As CheckBox = DirectCast(grdRoster.Rows(i).Cells(0).FindControl("Delete_Row"), CheckBox)
FindControl已不是System.Windows.Forms的成員.DataGridViewCell。另外,我非常確定整行是錯誤的,因爲複選框 位於設置爲ColumnType:DataGridViewCheckBoxColumn的datagrid列中,並不是真正的單獨控件。
如何在winform上得到相同的結果?這是我的整個代碼。
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
'Create String Collection to store
'IDs of records to be deleted
Dim ticketNumberCollection As New StringCollection()
Dim strTicketNumber As String = String.Empty
'Loop through GridView rows to find checked rows
For i As Integer = 0 To grdRoster.Rows.Count - 1
Dim chkDelete As CheckBox = DirectCast(grdRoster.Rows(i).Cells(0).FindControl("Delete_Row"), CheckBox)
If chkDelete IsNot Nothing Then
If chkDelete.Checked Then
strTicketNumber = grdRoster.Rows(i).Cells(1).ToString
ticketNumberCollection.Add(strTicketNumber)
End If
End If
Next
'Call the method to Delete records
DeleteMultipleRecords(ticketNumberCollection)
' rebind the GridView
grdRoster.DataBind()
End Sub
' Sub to delete multiple records
' @param "idCollection" calls the string collection above
' and deletes the selected record separated by ","
Private Sub DeleteMultipleRecords(ByVal ticketNumberCollection As StringCollection)
Dim IDs As String = ""
'Create string builder to store
'delete commands separated by ,
For Each id As String In ticketNumberCollection
IDs += id.ToString() & ","
Next
Try
Dim strTicketID As String = IDs.Substring(0, IDs.LastIndexOf(","))
DataSheetTableAdapter.DeleteRecord(strTicketID)
Catch ex As Exception
Dim errorMsg As String = "Error in Deletion"
errorMsg += ex.Message
Throw New Exception(errorMsg)
Finally
Me.Close()
End Try
End Sub
您上面的代碼已經識別並處理了要刪除的記錄。什麼是你準確得到的錯誤或問題?爲什麼要翻譯ASP.NET代碼?它會去哪裏? – NoChance
@EmmadKareem正在進入winform。錯誤在於FindControl不是系統的成員。 Windows.Forms.Datagrid –
當然不是,我的問題是關於你的程序要做什麼,把翻譯問題放在一邊。你爲什麼需要這條線?它應該提供什麼樣的價值? – NoChance