我在Windows窗体中有DataGridView,其中包含一些数据,我有编辑按钮,我想使用TextBox和按钮搜索行,当我找到想要的行时,我会更改它并单击编辑按钮,当我编辑任何行(不使用搜索按钮)并按编辑数据库更新时,我的问题是:当我搜索特定行并找到它然后编辑数据并按下编辑按钮数据库中的数据&# 39; t更新,请帮助,我使用此代码:
Imports System.Data.SqlClient
Public Class Form9
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Clinic System\Clinic System\ClinicDB.mdf;Integrated Security=True;User Instance=True")
Private cmdSelect, cmdDelete As String
Private daEmployees As New SqlDataAdapter("Select * From History", sqlConn)
Private sqlCmndBuilder As New SqlCommandBuilder(daEmployees)
Private myDS As New DataSet
Private Sub HistoryBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.HistoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ClinicDBDataSet3)
End Sub
Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
daEmployees.Fill(myDS, "History")
HistoryDataGridView.DataSource = myDS.Tables(0)
End Sub
Private Sub ButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click
Try
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Clinic System\Clinic System\ClinicDB.mdf;Integrated Security=True;User Instance=True")
Dim d1 As New SqlDataAdapter("Select * from History Where Name like '%" & TextBox1.Text & "%'", con)
Dim d2 As New DataTable
d1.Fill(d2)
HistoryDataGridView.DataSource = d2
Catch ex As Exception
MessageBox.Show("Err.Discription")
End Try
End Sub
Private Sub ButtonEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEdit.Click
daEmployees.Update(myDS.Tables(0))
MsgBox("Patient Details Updated!")
End Sub
答案 0 :(得分:0)
您的问题很简单 - 您尝试更新错误的表格
daEmployees.Update(myDS.Tables(0))
您需要更新d2
在表单范围
Dim _isSearch as boolean
在Form9_Load
上以及重新加载时
_isSearch = false
在这里你可以做什么 - 在ButtonSearch_Click
_isSearch = true
If myDS.Tables.Contains("SEARCH_TBL") Then
myDS.Tables.Remove("SEARCH_TBL")
End if
Dim d2 As DataTable = myDS.Tables.Add("SEARCH_TBL")
d1.Fill(d2)
在ButtonEdit_Click
if _isSearch then
daEmployees.Update(myDS.Tables("SEARCH_TBL"))
else
daEmployees.Update(myDS.Tables(0))
End if
我认为,daEmployees
应该更新“SEARCH_TBL”,因为结果集与第一个表相同。如果没有,只需将您的其他适配器带到表单范围。
但实际上,您可以重复使用网格和更新按钮,但是您需要创建逻辑跟踪当前加载的表。