【问题标题】:Deleting multiple records from sql bound Datagrid从 sql 绑定的 Datagrid 中删除多条记录
【发布时间】: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
  • 当然不是,我的问题是你想让你的程序做什么,把翻译问题放在一边。为什么需要这条线?它应该提供什么价值?

标签: vb.net winforms datagrid


【解决方案1】:

要从数据绑定的网格视图中删除多条记录,您应该在运行时创建 DataGridViewCheckBoxColumn

    Dim chk As New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Add(chk)
            chk.HeaderText = "Select"

'然后将你的datagridview与数据集绑定

 Dim sql As String = "SELECT * FROM table_name"
    ' Dim connection As New SqlConnection(connectionString)
    conn.Open()
    sCommand = New SqlCommand(sql, conn)
    sAdapter = New SqlDataAdapter(sCommand)
    sBuilder = New SqlCommandBuilder(sAdapter)
    sDs = New DataSet()
    sAdapter.Fill(sDs, "table_name")
    sTable = sDs.Tables("table_name")

 DataGridView1.DataSource = sDs.Tables("table_name")

'然后遍历每一列,得到检查的值

Try
        DataGridView1.EndEdit()
        For j = Me.DataGridView1.Rows.Count - 1 To 0 Step -1
            If Not IsDBNull(DataGridView1.Rows(j).Cells(0).Value) Then
                If DataGridView1.Rows(j).Cells(0).Value = True Then
                    check = True
                    If MessageBox.Show("Do you want to delete these records?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                        For i = Me.DataGridView1.Rows.Count - 1 To 0 Step -1
                            If Not IsDBNull(DataGridView1.Rows(i).Cells(0).Value) Then
                                If DataGridView1.Rows(i).Cells(0).Value = True Then

                                 'remove the checked columns and update datatable
                                    DataGridView1.Rows.RemoveAt(i)
                                    sAdapter.Update(sTable)
                                End If
                            End If
                        Next
                    Else
                        Return
                    End If
                Else
                End If
            End If
        Next
        If check = False Then
            MsgBox("Nothing Selected")
        End If

                Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

【讨论】:

  • 很好的答案——如果 .Cells(0) 被编码为 .Cells("Select") 会更清楚一点
猜你喜欢
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 2021-11-23
  • 2020-08-27
  • 2013-01-30
  • 2010-12-27
相关资源
最近更新 更多