如何使用主键从列名中删除数据表中的数据行

时间:2014-05-20 05:27:34

标签: vb.net

我想从vb.net中的datantable中删除数据行。 当我根据具有主键的列找到该行时,它返回错误。我谷歌但无法解决错误。 这是我的代码

Private Sub frmMatching_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    dtFromCustomer = New DataTable()
    dtToCusotmer = New DataTable()
    dtTemp = New DataTable()
    SetColumn()

    BindFromCustomer()
    BindToCustomer()
End Sub

Private Sub SetColumn()
    Dim keyColumn As New DataColumn()
    keyColumn.ColumnName = "CV_CODE"
    keys(0) = keyColumn

    dtFromCustomer.Columns.Add(keyColumn)
    dtFromCustomer.Columns.Add("CV_NAME")

    dtFromCustomer.PrimaryKey = keys       

End Sub

Private Sub BindFromCustomer()
    Dim ds As New DataSet    
    ds.Clear()   

    ds = GetDataset(sql)

    Try
        If ds.Tables(0).Rows.Count > 0 Then
            dtFromCustomer = ds.Tables(0)
            dtFromCustomer.PrimaryKey = Nothing

            Dim col As DataColumn = dtFromCustomer.Columns("CV_CODE")

            dtFromCustomer.AcceptChanges()
            gvFromCustomer.DataSource = dtFromCustomer
        End If        
End Sub

 Private Sub btnAddOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddOne.Click
    If gvFromCustomer.SelectedRows.Count > 0 And dtFromCustomer.Rows.Count > 0 Then
        Dim dr As DataRow
        Dim i As Integer = 0       

        Try
            Dim foundRow As DataRow

            Dim findThese(0) As Object

            findThese(0) = gvFromCustomer.SelectedRows(0).Cells("CV_CODE").Value.ToString()

            foundRow = dtFromCustomer.Rows.Find(findThese)

            If Not (foundRow Is Nothing) Then 'row is found
                dtFromCustomer.Rows.Remove(foundRow)
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try          

        dtFromCustomer.AcceptChanges()
        gvFromCustomer.DataSource = dtFromCustomer
        gvToCustomer.Refresh()        

    End If

我的错误得到了这一行

 foundRow = dtFromCustomer.Rows.Find(findThese)

0 个答案:

没有答案