我已经使用复选框列构建了一个datagrid,以便用户可以选择要删除的记录。我发布了一个类似的 几天前的问题。
我只是设法让程序在中途工作。我写的过程仅在一条记录时删除 被选中。它不会删除整个列表。这是我的用户界面:
这是我的代码:
Private Sub btnDeleteRecord_Click(sender As Object, e As EventArgs) Handles btnDeleteRecord.Click
'This procedure deletes the selected ticket number
'from the DataSheet table. The procedure refreshes
'the Datasheet table and the labels in the frmMainForm
Dim idCollection As New StringCollection()
Dim strID As String = String.Empty
Try
'Store each selected record on string collection
For Each row As DataGridViewRow In grdDeleteRecord.Rows
If row.Cells(0).Value() Then
strID = row.Cells(1).Value()
idCollection.Add(strID)
End If
Next
'Call procedure to delete multiple records
DeleteMultipleRecords(idCollection)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub DeleteMultipleRecords(ByVal idCollection As StringCollection)
Dim IDs As String = ""
'Create string builder to store
'delete commands separated by ;
For Each id As String In idCollection
IDs += id.ToString() & ","
Next
Try
Dim strIDs As String = IDs.Substring(0, IDs.LastIndexOf(","))
Dim strPrompt As String = "Record deletion cannot be undone. Are you sure you want to delete the record?"
Dim strTitle As String = "Delete Record"
Dim msgProceed As MsgBoxResult
'Warn the user that records cannot be undone
msgProceed = MsgBox(strPrompt, CType(MsgBoxStyle.YesNo + MsgBoxStyle.Critical, MsgBoxStyle), strTitle)
If msgProceed = MsgBoxResult.Yes Then
'Local variables
Dim strTicketNumber As String = txtTicketNumber.Text
Dim todayDate As Date = Date.Now
Dim beginWeek As Date = Public_Subs.MondayOfWeek(todayDate).Date
Dim endOfWeek As Date = beginWeek.AddDays(6)
Dim strBeginDay As String = todayDate.ToShortDateString & " 12:00:00 AM"
Dim strEndDay As String = todayDate.ToShortDateString & " 11:59:59 PM"
Cursor.Current = Cursors.WaitCursor
DataSheetTableAdapter.DeleteRecord(strIDs)
Else
btnClear.Visible = False
btnDeleteRecord.Enabled = False
End If
Catch ex As Exception
Dim errorMsg As String = "Error in Deletion"
errorMsg += ex.Message
Throw New Exception(errorMsg)
Finally
End Try
End Sub
我在DeleteMultipleRecords子上放了一个消息框,这里是显示:
我认为问题是我的DataSheetTableAdapter.DeleteRecord(strIDs)
没有在每个逗号后分隔记录并迭代每个记录并删除。
有人能让我知道我在哪里错了吗?
答案 0 :(得分:1)
我会将此作为答案而不是评论发布,以便我可以包含代码。
1-将复选框列添加到设计器中的网格。我假设您已经知道如何做到这一点,但如果您不这样做,请告诉我们。
2-填充DataTable并通过BindingSource将其绑定到您的网格。
myAdapter.Fill(myDataTable)
Me.BindingSource1.DataSource = myDataTable
Me.DataGridView1.DataSource = Me.BindingSource1
如果您使用的是类型化数据集,则可以在设计器中设置绑定。
3-选中相应的框后,从绑定的DataTable中删除相应的行。
Dim rowsToDelete = (From row In Me.DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where CBool(row.Cells(0).Value)
Select row.DataBoundItem).Cast(Of DataRowView)().ToArray()
For Each row In rowsToDelete
row.Delete()
Next
这可以通过其他方式完成,无论是否有LINQ,但你希望你能得到这个想法。
4-使用相同的数据/表适配器将所有更改保存回数据库。
myAdapter.Update(myDataTable)
答案 1 :(得分:0)
使用连接模式,您可以根据所选的票证编号循环遍历DataGridView的选定行和从数据库中删除。
Private Sub DeleteSelected(ByVal UserSelectedID As Integer)
Using CN As New OleDbConnection With {.ConnectionString = GetBuilderCNString()}
CN.Open()
Dim SqlStr As String =
("DELETE * FROM Tickets WHERE TicketNumber=?")
Using DelCMD As New OleDbCommand With _
{.Connection = CN, .CommandType = CommandType.Text, .CommandText = SqlStr}
Try
Dim I As Integer = 0
For Each Irow As DataGridViewRow In DisplayDGV.SelectedRows
UserSelectedID = Irow.Cells(0).Value 'TicketNumber
With DelCMD.Parameters
.Add("?", OleDbType.BigInt).Value = UserSelectedID
End With
DelCMD.ExecuteNonQuery()
DelCMD.Parameters.Clear()
I += 1
Next
LblStatus.Text = (I & " Deleted successfully.")
Catch ex As OleDbException
Debug.WriteLine("Delete Error : " & ex.Message)
End Try
End Using
End Using
End Sub