【发布时间】:2014-02-02 00:00:30
【问题描述】:
下面的过程允许我通过选中我的数据网格上的复选框来一次删除多条记录。该程序是在 ASP.net 上编写的,但现在我在 VB.net 上使用 winform。
我有一个列名为“删除”的数据网格,复选框所在的位置。用户会检查 它要删除的记录以及将删除这些记录。我使用“票号”列值作为查询的参数。
我遇到的问题是,因为它是为 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 的数据网格列上,并不是真正的单独控件。
如何在 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 代码?它会去哪里?
-
@EmmadKareem 正在进入 winform。错误是 FindControl 不是系统的成员。 Windows.Forms.Datagrid
-
当然不是,我的问题是你想让你的程序做什么,把翻译问题放在一边。为什么需要这条线?它应该提供什么价值?