vb.net 带有 ODBC 数据适配器的 DataGridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14141051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView with ODBC data adapter
提问by Wine Too
I am load data from database to my datagridview through ODBC adapter.
我通过 ODBC 适配器将数据从数据库加载到我的 datagridview。
cmd = New Odbc.OdbcCommand(sql, cn)
adp = New Odbc.OdbcDataAdapter(cmd)
adp.Fill(ds, "temp2")
bs.DataSource = ds
DataGridView2.DataSource = bs
That way I can change and update data in database "lively".
But I have different situation now.
For changing data I have to go on grid's doubleclick to another form and when I come back I would like that my datagridview show changes in certain row.
这样我就可以“生动地”更改和更新数据库中的数据。
但我现在有不同的情况。
为了更改数据,我必须双击网格到另一个表单,当我回来时,我希望我的 datagridview 显示特定行中的更改。
This is what I try:
这是我的尝试:
Dim fl As New dataform
With fl
.StartPosition = FormStartPosition.Manual
.aCallerLocation = Me.Location
.ShowDialog()
End With
fl = Nothing
Dim c_builder As New Odbc.OdbcCommandBuilder(adp)
Dim o As Integer
Try
o = adp.Update(ds, "temp2")
MsgBox(o)
Catch ex As Exception
MsgBox(ex.Message)
End Try
But I can't simply get it whole day! There is no exception generated but "o" is allways zero.
但我不能简单地得到它一整天!没有异常生成,但“o”始终为零。
What do I do wrong and how to get this functionality to see changes in the row after returnimg from "dataform"?
我做错了什么以及如何获得此功能以查看从“数据表单”返回后的行中的更改?
回答by Calma
Try this
尝试这个
cmd = New Odbc.OdbcCommand(sql, cn)
adp = New Odbc.OdbcDataAdapter(cmd)
Dim c_builder As New Odbc.OdbcCommandBuilder(adp)
adp.Fill(ds, "temp2")
bs.DataSource = ds
DataGridView2.DataSource = bs
and this
和这个
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim dsChange As DataSet = New DataSet
'add record
If (ds.HasChanges(DataRowState.Added)) Then
dsChange = ds.GetChanges(DataRowState.Added)
Dim rowchange As Integer
rowchange = adp.Update(dsChange, "temp2")
If (rowchange > 0) Then
MessageBox.Show(rowchange.ToString() & " Record Berhasil Dimasukan")
End If
End If
'Modified record
If (ds.HasChanges(DataRowState.Modified)) Then
dsChange = ds.GetChanges(DataRowState.Modified)
Dim rowchange As Integer
rowchange = adp.Update(dsChange, "temp2")
If (rowchange > 0) Then
MessageBox.Show(rowchange.ToString() & " Record Berhasil Dihapus")
End If
End If
'Delete record
If (ds.HasChanges(DataRowState.Deleted)) Then
dsChange = ds.GetChanges(DataRowState.Deleted)
Dim rowchange As Integer
rowchange = adp.Update(dsChange, "temp2")
If (rowchange > 0) Then
MessageBox.Show(rowchange.ToString() & " Record Berhasil Diubah")
End If
End If
'Menerapkan perubahan
ds.AcceptChanges()
'Refresh(DataGrid)
DataGridView2.Refresh()
End Sub

