DataGridView绑定到SQL - 以编程方式添加行

时间:2016-10-25 12:28:03

标签: sql datagridview bindingsource

我有一个DataGridView,我从SQL查询填充(如果表中已有任何数据)。我有一个保存和更新按钮,如果进行了更改,用户可以保存任何更改或提出任何更改。然而..

我有另一个按钮,将它们带到另一个表单,在那里他们可以输入要插入DataGridView的细节。这是我遇到问题的地方,我一直收到以下错误:

  

当控件是数据绑定时,无法以编程方式将行添加到datagridview的行集合

我的代码摘录如下,有什么想法吗?我需要DataGridView可以编辑,其中Save按钮更新SQL表,但我还需要能够从另一个表单中插入行,其中Save按钮也会更新(插入)这个。

表格1代码:

Imports System.Data

Public Class AuditForm

Private DAdapter As New SqlClient.SqlDataAdapter()
Private BSource1 As New BindingSource()


Private Sub AuditForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim EmployeeDataset As New DataSet
    Dim sSQL As String
    If LoadSettings() = False Then
        MsgBox("Error: Unable to Load Settings", vbOKOnly + vbCritical)
        Me.Close()
    End If
    With DG_Audit
        'General GridView Settings
        .AutoGenerateColumns = False
        .AllowUserToAddRows = False
        .AllowUserToDeleteRows = True
        .AllowUserToOrderColumns = False
        .MultiSelect = False
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .ReadOnly = False
        .DataSource = BSource1

        'General GridView Formatting
        .RowsDefaultCellStyle.BackColor = Color.White
        .AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
        .RowHeadersDefaultCellStyle.SelectionBackColor = Color.Khaki
        .DefaultCellStyle.SelectionBackColor = Color.Khaki
        .DefaultCellStyle.SelectionForeColor = Color.Black
    End With

End Sub




Private Sub BTLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTLoad.Click
    GetData("SELECT * FROM AdminCS_Audit WHERE Audit_CUser = '" & AdminCB.Text & "' " &
            "AND Audit_Submitted IS NULL")
End Sub

Private Sub BTSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTSave.Click
    DAdapter.Update(CType(BSource1.DataSource, DataTable))
    'DAdapter.Update(CType(DG_Audit.DataSource, DataTable))
End Sub

Sub GetData(ByRef cmd As String)
    Try
        Dim cnt As String = gConnection
        DAdapter = New SqlClient.SqlDataAdapter(cmd, cnt)
        Dim cmdBuilder As New SqlClient.SqlCommandBuilder(DAdapter)
        Dim AuditData As New DataTable()
        AuditData.Locale = System.Globalization.CultureInfo.InvariantCulture
        DAdapter.Fill(AuditData)
        BSource1.DataSource = AuditData
    Catch ex As Exception
        MsgBox(ex.Message, vbOKOnly + vbCritical, "SQL Error")
    End Try
End Sub

End Class

表单2代码,它将添加新行: Imports System.Data

Public Class AuditDetailAdd
Private Sub btOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
    Dim PassFail As String
    If RB_Passed.Checked = True Then
        PassFail = "Pass"
    ElseIf RB_Failed.Checked = True Then
        PassFail = "Fail"
    Else
        PassFail = ""
    End If

    AuditForm.DG_Audit.Rows.Add(Nothing, Nothing, TickForm.AdminCB.Text, CB_Field.Text, L_Action.Text, L_Time.Text, TB_Reason.Text, PassFail, TB_Comments.Text)
    Me.Close()
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我最终得到了以下工作:

Private Sub btOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
    Dim PassFail As String
    If RB_Passed.Checked = True Then
        PassFail = "Pass"
    ElseIf RB_Failed.Checked = True Then
        PassFail = "Fail"
    Else
        PassFail = ""
    End If
    Dim DR As DataRow = CType(TickForm.BSource.DataSource, DataTable).NewRow
    DR.Item(0) = DBNull.Value
    DR.Item(1) = Date.Today
    DR.Item(2) = TickForm.AdminCB.Text
    DR.Item(3) = CB_Field.Text
    DR.Item(4) = L_Action.Text
    DR.Item(5) = DateTime.Parse(L_Time.Text)
    If Not String.IsNullOrEmpty(TB_Reason.Text) Then
        DR.Item(6) = TB_Reason.Text
    Else
        DR.Item(6) = DBNull.Value
    End If
    DR.Item(7) = PassFail
    If Not String.IsNullOrEmpty(TB_Comments.Text) Then
        DR.Item(8) = TB_Comments.Text
    Else
        DR.Item(8) = DBNull.Value
    End If
    CType(TickForm.BSource.DataSource, DataTable).Rows.Add(DR)

    Me.Close()
End Sub