从文本框更新数据库和datagridview

时间:2018-03-22 19:22:28

标签: vb.net datagridview textbox

我的应用程序在页面的下半部分有一个datagridview,上半部分有文本框,组合框,按钮等。 当用户更改网格中突出显示的行时,它会在上半部分的对象中显示该行的所有信息。 如果用户想要连续更改数据,则他单击编辑按钮,该按钮启用文本框等...并允许用户编辑数据。 (以上所有工作都很好。)

当他想保存更改时,他会点击保存按钮。 这应该将数据源更新到网格并显示网格中的更改。 但是,我一直无法做到这一点。

任何帮助表示赞赏。 代码如下:

Imports System.Data.SqlClient

Public Class frmEmployeeInformation
Public SQL As New SQLControl()
Dim strEditType As String

Private Sub frmEmployeeInformation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    LoadGrid()
End Sub

Public Sub LoadGrid(Optional query As String = "")
    If query = "" Then
        SQL.ExecuteQuery("Select * from EmployeeInformation;")
    Else
        SQL.ExecuteQuery(query)
    End If
    If SQL.HasException(True) Then Exit Sub
    dgvEmployeeInformation.DataSource = SQL.DBDS.Tables(0)
    dgvEmployeeInformation.Rows(0).Selected = True
    SQL.DBDA.UpdateCommand = New SqlClient.SqlCommandBuilder(SQL.DBDA).GetUpdateCommand
End Sub

Private Sub DisplayValues()
    If dgvEmployeeInformation.RowCount > 2 Then
        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("FirstName").Value <> Nothing Then
            txtFirstName.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("FirstName").Value.ToString 'EmployeeInformation.FirstName
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("LastName").Value <> Nothing Then
            txtLastName.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("LastName").Value.ToString 'EmployeeInformation.LastName
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SSN").Value IsNot Nothing Then
            txtSSN.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SSN").Value.ToString 'EmployeeInformation.SSN
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeInformationID").Value <> Nothing Then
            txtEmployeeID.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeInformationID").Value.ToString 'EmployeeInformation.EmployeeInformationID
        End If

        'If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("ADPID").Value <> Nothing Then
        'txtADPID.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("ADPID").Value.ToString 'EmployeeInformation.ADPID
        'End If

        'If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("VP").Value <> Nothing Then
        'cboVP.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("VP").Value.ToString 'EmployeeInformation.VP
        'End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("Default_LocationID").Value <> Nothing Then
            cboDefaultLocation.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("Default_LocationID").Value.ToString 'EmployeeInformation.DefaultLocation
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SystemTableID").Value <> Nothing Then
            txtTableID.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SystemTableID").Value.ToString 'EmployeeInformation.TableID
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeActive").Value <> Nothing Then
            chkbxActive.Checked = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeActive").Value.ToString 'EmployeeInformation.EmployeeActive
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("PrimaryFile").Value <> Nothing Then
            chkbxPrimaryFile.Checked = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("PrimaryFile").Value.ToString 'EmployeeInformation.PrimaryFile
        End If

        If dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("UnallocatedTime").Value <> Nothing Then
            chkbxUnallocatedTime.Checked = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("UnallocatedTime").Value.ToString 'EmployeeInformation.UnallocatedTime
        End If
    End If
End Sub
Private Sub ChangeButtons()
    btnClose.Enabled = Not btnClose.Enabled
    btnSave.Enabled = Not btnSave.Enabled
    btnClear.Enabled = Not btnClear.Enabled
    btnFind.Enabled = Not btnFind.Enabled
    btnCancel.Enabled = Not btnCancel.Enabled
    btnEdit.Enabled = Not btnEdit.Enabled
    btnAdd.Enabled = Not btnAdd.Enabled
    btnCopy.Enabled = Not btnCopy.Enabled
End Sub

Private Sub ChangeFields()
    chkbxActive.Enabled = Not chkbxActive.Enabled
    chkbxPrimaryFile.Enabled = Not chkbxPrimaryFile.Enabled
    chkbxUnallocatedTime.Enabled = Not chkbxUnallocatedTime.Enabled
    txtTableID.Enabled = Not txtTableID.Enabled
    txtADPID.Enabled = Not txtADPID.Enabled
    cboVP.Enabled = Not cboVP.Enabled
    cboDefaultLocation.Enabled = Not cboDefaultLocation.Enabled
End Sub

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
    Me.Close()
End Sub

Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
    If txtEmployeeID.Text <> "" Then
        SQL.AddParam("@EmployeeInformationID", txtEmployeeID.Text)
        LoadGrid("select * from EmployeeInformation where EmployeeInformationID = @EmployeeInformationID;")
    ElseIf txtSSN.Text <> "" Then
        SQL.AddParam("@SSN", txtSSN.Text)
        LoadGrid("select * from EmployeeInformation where SSN = @SSN;")
    ElseIf txtFirstName.Text <> "" And txtLastName.Text <> "" Then
        SQL.AddParam("@FirstName", txtFirstName.Text)
        SQL.AddParam("@LastName", txtLastName.Text)
        LoadGrid("select * from EmployeeInformation where FirstName = @FirstName and LastName = @LastName;")
    Else
        LoadGrid("Select * from EmployeeInformation;")
    End If
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    txtFirstName.Text = ""
    txtLastName.Text = ""
    txtSSN.Text = ""
    txtEmployeeID.Text = ""
    txtADPID.Text = ""
    cboVP.Text = ""
    cboDefaultLocation.Text = ""
    txtTableID.Text = ""
    chkbxActive.Checked = "False"
    chkbxPrimaryFile.Checked = "False"
    chkbxUnallocatedTime.Checked = "False"
End Sub

Private Sub dgvEmployeeInformation_DoubleClick(sender As Object, e As EventArgs) Handles dgvEmployeeInformation.DoubleClick
    DisplayValues()
    ChangeFields()
    ChangeButtons()
End Sub

Private Sub dgvEmployeeInformation_SelectionChanged(sender As Object, e As EventArgs) Handles dgvEmployeeInformation.SelectionChanged
    DisplayValues()
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    If strEditType = "Edit" Then
        ' The code below updates the grid but the changes are not saved to the database. Probably not the way to go
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("FirstName").Value = txtFirstName.Text 'EmployeeInformation.FirstName
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("LastName").Value = txtLastName.Text 'EmployeeInformation.LastName
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SSN").Value = txtSSN.Text 'EmployeeInformation.SSN
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeInformationID").Value = txtEmployeeID.Text 'EmployeeInformation.EmployeeInformationID
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("Default_LocationID").Value = cboDefaultLocation.Text 'EmployeeInformation.DefaultLocation
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SystemTableID").Value = txtTableID.Text 'EmployeeInformation.TableID
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeActive").Value = chkbxActive.Checked 'EmployeeInformation.EmployeeActive
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("PrimaryFile").Value = chkbxPrimaryFile.Checked 'EmployeeInformation.PrimaryFile
        dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("UnallocatedTime").Value = chkbxUnallocatedTime.Checked 'EmployeeInformation.UnallocatedTime
        ' The code above updates the grid but the changes are not saved to the database.
        dgvEmployeeInformation.EndEdit()
        SQL.DBDS.Tables(0).AcceptChanges()
        SQL.DBDA.Update(SQL.DBDS)
        'txtADPID.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("ADPID").Value.ToString 'EmployeeInformation.ADPID
        'cboVP.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("VP").Value.ToString 'EmployeeInformation.VP
    End If
    ChangeFields()
    ChangeButtons()
    strEditType = ""
    'LoadGrid()
End Sub

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    ChangeFields()
    ChangeButtons()
    strEditType = ""
End Sub

Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
    dgvEmployeeInformation_DoubleClick(Nothing, EventArgs.Empty)
    strEditType = "Edit"
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    btnClear_Click(Nothing, EventArgs.Empty)
    txtFirstName.Focus()
    strEditType = "Add"
    ChangeFields()
    ChangeButtons()

End Sub

Private Sub btnCopy_Click(sender As Object, e As EventArgs) Handles btnCopy.Click
    cboDefaultLocation.Focus()
    strEditType = "Copy"
    ChangeFields()
    ChangeButtons()

End Sub
End Class

2 个答案:

答案 0 :(得分:0)

尝试另一种方式!!

我建议你,而不是这样做:

If strEditType = "Edit" Then
    ' The code below updates the grid but the changes are not saved to the database. Probably not the way to go
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("FirstName").Value = txtFirstName.Text 'EmployeeInformation.FirstName
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("LastName").Value = txtLastName.Text 'EmployeeInformation.LastName
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SSN").Value = txtSSN.Text 'EmployeeInformation.SSN
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeInformationID").Value = txtEmployeeID.Text 'EmployeeInformation.EmployeeInformationID
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("Default_LocationID").Value = cboDefaultLocation.Text 'EmployeeInformation.DefaultLocation
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("SystemTableID").Value = txtTableID.Text 'EmployeeInformation.TableID
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("EmployeeActive").Value = chkbxActive.Checked 'EmployeeInformation.EmployeeActive
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("PrimaryFile").Value = chkbxPrimaryFile.Checked 'EmployeeInformation.PrimaryFile
    dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("UnallocatedTime").Value = chkbxUnallocatedTime.Checked 'EmployeeInformation.UnallocatedTime
    ' The code above updates the grid but the changes are not saved to the database.
    dgvEmployeeInformation.EndEdit()
    SQL.DBDS.Tables(0).AcceptChanges()
    SQL.DBDA.Update(SQL.DBDS)
    'txtADPID.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("ADPID").Value.ToString 'EmployeeInformation.ADPID
    'cboVP.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("VP").Value.ToString 'EmployeeInformation.VP
End If

首先更新数据库,然后从数据库更新数据网格。

类似

Sql = "update TABLE_Name set FirstName = @fName" 'write all your query'    
myCmd = New SqlCommand(Sql, myConn)
myCmd.Parameters.Add("@fName", SqlDbType.VarChar).Value = txtFirstName.Text 
myCmd.ExecuteNonQuery()
MsgBox("Update complete.")
myConn.Close()

而且你可以更新你的数据网格!我总是习惯将DataGrid连接到我的SQL表,因此代码将是:

Public Sub Updating()
    Me.Table_NameTableAdapter.Fill(Me.DB_NameDataSet1.Table_Name)
End Sub

答案 1 :(得分:0)

让它发挥作用:

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim dbconn As New SqlConnection("server=192.168.50.205;Database=SEMHC_Admin2018;trusted_Connection=True;")
    Dim mycmd As SqlCommand
    Dim mySQL As String

    dbconn.Open()

    If strEditType = "Add" Or strEditType = "Copy" Then
    End If

    If strEditType = "Edit" Then
        mySQL = "update employeeInformation 
                            set FirstName = @firstName,
                                LastName = @LastName,
                                SSN = @SSN,
                                EmployeeInformationID = @EmployeeInformationID,
                                Default_LocationID = @Default_LocationID,
                                EmployeeActive = @chkbxActive,
                                PrimaryFile = @chkbxPrimaryFile,
                                UnallocatedTime = @chkbxUnallocatedTime
                                from employeeInformation
                                WHERE SystemTableID = @SystemTableID
                         " 'write query'    
        mycmd = New SqlCommand(mySQL, dbconn)
        mycmd.Parameters.Add("@SystemTableID", SqlDbType.VarChar).Value = txtTableID.Text
        mycmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text
        mycmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text
        mycmd.Parameters.Add("@SSN", SqlDbType.VarChar).Value = txtSSN.Text
        mycmd.Parameters.Add("@EmployeeInformationID", SqlDbType.VarChar).Value = txtEmployeeID.Text
        mycmd.Parameters.Add("@Default_LocationID", SqlDbType.VarChar).Value = cboDefaultLocation.Text
        mycmd.Parameters.Add("@chkbxActive", SqlDbType.VarChar).Value = chkbxActive.Checked
        mycmd.Parameters.Add("@chkbxPrimaryFile", SqlDbType.VarChar).Value = chkbxPrimaryFile.Checked
        mycmd.Parameters.Add("@chkbxUnallocatedTime", SqlDbType.VarChar).Value = chkbxUnallocatedTime.Checked
        mycmd.ExecuteNonQuery()
        'MsgBox("Update complete.")
        dbconn.Close()
        'SQL.DBDS.Tables(0).AcceptChanges()
        'SQL.DBDA.Update(SQL.DBDS)
        'txtADPID.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("ADPID").Value.ToString 'EmployeeInformation.ADPID
        'cboVP.Text = dgvEmployeeInformation.Rows(dgvEmployeeInformation.CurrentRow.Index).Cells("VP").Value.ToString 'EmployeeInformation.VP
    End If
    ChangeFields()
    ChangeButtons()
    strEditType = ""
    LoadGrid()
End Sub