VB.net 中DataGridView控件上修改数据表如何保存到Access中去?
这是我的代码,你看有什么问题?如果回答的号的话,我再加100分!谢谢!DimAAADataSet1AsNewDataSetAAADataSet1.Clear()Dimco...
这是我的代码,你看有什么问题?如果回答的号的话,我再加100分!谢谢!
Dim AAADataSet1 As New DataSet
AAADataSet1.Clear()
Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection
connection.ConnectionString = "provider=Microsoft.jet.oledb.4.0;Data source= AAA.mdb"
Dim shujubiaoadapter As New OleDb.OleDbDataAdapter
connection.Open()
Try
If AAADataSet1.HasChanges Then
HuaxueTableAdapter.Update(AAADataSet1.Tables("huaxue"))
'shujubiaoadapter.Update(AAADataSet1.GetChanges)
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("修改成功!")
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("无修改的内容!")
End If
Catch ex As Exception
MessageBox.Show("修改出错!")
End Try 展开
Dim AAADataSet1 As New DataSet
AAADataSet1.Clear()
Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection
connection.ConnectionString = "provider=Microsoft.jet.oledb.4.0;Data source= AAA.mdb"
Dim shujubiaoadapter As New OleDb.OleDbDataAdapter
connection.Open()
Try
If AAADataSet1.HasChanges Then
HuaxueTableAdapter.Update(AAADataSet1.Tables("huaxue"))
'shujubiaoadapter.Update(AAADataSet1.GetChanges)
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("修改成功!")
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("无修改的内容!")
End If
Catch ex As Exception
MessageBox.Show("修改出错!")
End Try 展开
2个回答
展开全部
贴完整代码。
AAADataSet1绑定DataGridView了?那里绑定的?
如果这是一段完整更新代码,
AAADataSet1.Clear()
你上来就把他清空了,更新啥啊?
AAADataSet1绑定DataGridView了?那里绑定的?
如果这是一段完整更新代码,
AAADataSet1.Clear()
你上来就把他清空了,更新啥啊?
追问
我去了这一行也不行啊!我修改后照样会出现"无修改的内容!"对话框,我的数据库绑定了啊!
追答
去了这一行当然不行。
Dim AAADataSet1 As New DataSet
你直接新建了个AAADataSet ,同时就拿它作为更新数据了。里面哪里来的数据
AAADataSet1 应该在formload的或则什么比如新建啊编辑啊的按钮按下的时候创建。然后等待数据的数据,完成后再由更新代码进行更新。
仔细想下流程和逻辑,写程序就是写个逻辑,知道什么时候做什么样的事情。
展开全部
er("Select * from xs", conn);
mysql.Fill(myTable);
dataGrid1.DataSource = myTable;
cmOrders = (CurrencyManager)BindingContext[myTable];
cb = new SqlCommandBuilder(mysql);//有这条才能使用更新命令.表中必须有主键..
更新的代码
cmOrders.EndCurrentEdit();
if (myTable.GetChanges() != null)
{
try
{
mysql.Update(myTable);
MessageBox.Show("数据保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
myTable.RejectChanges();
}
}
注:声明~~~~`````
private static DataTable myTable;
private CurrencyManager cmOrders;
SqlDataAdapter mysql;
private SqlCommandBuilder cb;
mysql.Fill(myTable);
dataGrid1.DataSource = myTable;
cmOrders = (CurrencyManager)BindingContext[myTable];
cb = new SqlCommandBuilder(mysql);//有这条才能使用更新命令.表中必须有主键..
更新的代码
cmOrders.EndCurrentEdit();
if (myTable.GetChanges() != null)
{
try
{
mysql.Update(myTable);
MessageBox.Show("数据保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
myTable.RejectChanges();
}
}
注:声明~~~~`````
private static DataTable myTable;
private CurrencyManager cmOrders;
SqlDataAdapter mysql;
private SqlCommandBuilder cb;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询