SQL 쿼리에서 채우는 DataGridView가 있습니다 (이미 테이블에 데이터가있는 경우). 변경 사항이 저장되면 사용자가 변경 사항을 저장하거나 생성 된 내용을 가져올 수있는 저장 및 업데이트 버튼이 있습니다. 그러나 ..DataGridView SQL에 바인드 됨 - 프로그래밍 방식으로 행 추가
DataGridView에 삽입 할 세부 정보를 입력 할 수있는 또 다른 단추가있는 단추가 있습니다. 문제가 발생하는 곳에서 아래 오류가 계속 발생합니다.
Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound
내 코드의 압축은 무엇입니까? 저장 버튼이 SQL 테이블을 업데이트하는 곳에서 편집 할 수 있도록 DataGridView가 필요하지만 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
양식이 코드 새로운 행에서 추가됩니다됩니다 수입 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