2014-01-14 45 views
0

下面的過程允許我通過選中我的數據網格中的複選框來一次刪除多條記錄。該程序是在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 
+0

您上面的代碼已經識別並處理了要刪除的記錄。什麼是你準確得到的錯誤或問題?爲什麼要翻譯ASP.NET代碼?它會去哪裏? – NoChance

+0

@EmmadKareem正在進入winform。錯誤在於FindControl不是系統的成員。 Windows.Forms.Datagrid –

+0

當然不是,我的問題是關於你的程序要做什麼,把翻譯問題放在一邊。你爲什麼需要這條線?它應該提供什麼樣的價值? – NoChance

回答

1

,是從數據綁定的GridView刪除多條記錄,你應該在運行時創建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 
+0

好的答案 - 如果.Cells(0)被編碼爲.Cells(「Select」),會更清楚一些。 – rheitzman