我想通过单击按钮在数据库视图中保存数据库中的一些更改。基本上相同的按钮必须进行我想要的更改(在下面的代码中使用循环)并自动保存它们但它不起作用。
你可以看看有什么不对吗?我认为这是关于更新线的事情。我在设计视图中的tableadapter命令上创建了更新字符串。如果您需要更多代码,请问我。评论的行是我正在做的一些测试。如果在编辑后更改单元格,则会保存。我使用了DGV5_SelectionChanged
事件。谢谢
Public Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.Text = DGV1(5, 0).Value
Dim i As Integer
Dim j As Integer
'loop para preencher a coluna do potencial de prescrição.... = 5
For i = 0 To DGV3.Rows.Count - 1
Select Case DGV3(2, i).Value
Case 26
DGV3(9, i).Value = 8
End Select
Next
DoctorEnterpriseDetailsTableAdapter.Update(GSM81DataSet.DoctorEnterpriseDetails)
'Loop para nr de clinicas
For i = 0 To DGV1.Rows.Count - 2
For j = 0 To DGV5.Rows.Count - 2
'guardar nas observações
If Trim(DGV1(20, i).Value.ToString) = "" Then
DGV1(20, i).Value = checknull(DGV5(6, j).Value.ToString) & " - " & checknull(DGV5(7, j).Value.ToString) & " - " & checknull(DGV5(8, j).Value.ToString)
Else
DGV1(20, i).Value = checknull(DGV1(20, i).Value) & Chr(13) & Chr(10) & checknull(DGV5(6, j).Value.ToString) & " - " & checknull(DGV5(7, j).Value.ToString) & " - " & checknull(DGV5(8, j).Value.ToString)
End If
DGV1(13, i).Value = 0
Next
Next
DoctorBasicTableAdapter.Update(GSM81DataSet.DoctorBasic)
'loop para a selecção (EnterpriseDetails) id 8 para nao aplicavel
For i = 0 To DGV3.Rows.Count - 1
Select Case DGV3(2, i).Value 'lusovencor
Case 26
DGV3(5, i).Value = 48
End Select
Next
DoctorEnterpriseDetailsTableAdapter.Update(GSM81DataSet.DoctorEnterpriseDetails)
' loop para a morada - Funciona!
For j = 0 To DGV5.Rows.Count - 2
DGV5(6, j).Value = "NÃO DEFINIDO" ' para a morada
DGV5(7, j).Value = 421705 'para o codigo postal
DGV5(8, j).Value = 0 'para o tipo local
DGV5(9, j).Value = "" ' para o horário
DGV5(10, j).Value = True ' visto para o LocalVisita
Next
DoctorsAddressTableAdapter.Update(GSM81DataSet.DoctorsAddress)
' loop perfil clinico - Funciona
For i = 0 To DGV3.Rows.Count - 2
Select Case DGV3(2, i).Value
Case 26
DGV3(23, i).Value = 8
End Select
Next
DoctorEnterpriseDetailsTableAdapter.Update(GSM81DataSet.DoctorEnterpriseDetails)
'loop categoria - medicos
For i = 0 To DGV3.Rows.Count - 2
Select Case DGV3(2, i).Value
Case 26
DGV3(6, i).Value = 84
End Select
Next
DoctorEnterpriseDetailsTableAdapter.Update(GSM81DataSet.DoctorEnterpriseDetails)
'sqlConnection1.Open()
'GSM81DataSet = DGV1(20, i).Value.DataSource
'DoctorBasicTableAdapter.Update(GSM81DataSet.DoctorBasic)
'GSM81DataSet.AcceptChanges()
ApagarRegistos()
'sqlConnection1.Close()
答案 0 :(得分:0)
这是一个示例程序,它允许您从数据网格视图中操作CRUD(创建/读取/更新和删除):
Imports System.Data.SqlClient
Public Class Form1
Dim sCommand As SqlCommand
Dim sAdapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim sDs As DataSet
Dim sTable As DataTable
Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM Stores"
Dim connection As New SqlConnection(connectionString)
connection.Open()
sCommand = New SqlCommand(sql, connection)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "Stores")
sTable = sDs.Tables("Stores")
connection.Close()
DataGridView1.DataSource = sDs.Tables("Stores")
DataGridView1.ReadOnly = True
save_btn.Enabled = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
DataGridView1.[ReadOnly] = False
save_btn.Enabled = True
new_btn.Enabled = False
delete_btn.Enabled = False
End Sub
Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click
If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
sAdapter.Update(sTable)
End If
End Sub
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
sAdapter.Update(sTable)
DataGridView1.[ReadOnly] = True
save_btn.Enabled = False
new_btn.Enabled = True
delete_btn.Enabled = True
End Sub
End Class
这允许在数据网格视图上进行基本的CRUD操作。