如何将数据网格视图的修改行更新到数据库? [英] How do I update a modified row of my datagridview to the db?

查看:95
本文介绍了如何将数据网格视图的修改行更新到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数据网格视图的修改行更新为数据库?

i已经尝试了很多...

但没有发生任何事情....



i 2天前问了这个问题....

可以帮助我......



尝试使用以下代码:但是......

 private void showButton_Click(object sender,EventArgs e)
{
con = new OleDbConnection(Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\\ETMS \\ETMS\\ETMS\\etms.accdb);
String query =SELECT * FROM Categories;
da = new OleDbDataAdapter(query,con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables [0];
con.Close();
}
private void UpdateButton_Click(object sender,EventArgs e)
{
da.Update(ds);
}



如果我运行代码

i我收到以下错误

更新需要一个按下带有修改行的datarow集合时的有效更新命令

请帮帮我......

谢谢

解决方案

< blockquote>这可能对你有帮助。,

 private void showButton_Click(object sender,EventArgs e)
{
con = new OleDbConnection(Provider = Microsoft .ACE.OLEDB.12.0;数据源= D:\\ETMS \\ETMS \\ETMS \\etms.accdb);
String query =SELECT * FROM Categories;
da = new OleDbDataAdapter(query,con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables [0];
con.Close();
}
private void UpdateButton_Click(object sender,EventArgs e)
{
DataSet ds1 =((DataSet)dataGridView1.DataSource)。GetChanges();
da.Update(ds1);
}


你可以这样做:



 私有  void  showButton_Click( object  sender,EventArgs e)
{
con = new OleDbConnection( Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\\ETMS \\ETMS\\ETMS\\etms.accdb );
字符串 query = SELECT * FROM Categories ;
da = new OleDbDataAdapter(query,con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
私有 void UpdateButton_Click( object sender,EventArgs e)
{
da.Update(dt);
}





更多信息请参考以下链接:



在DataGrid中编辑几乎任何内容 [ ^ ]



自动保存DataGridView行到SQL Server数据库 [ ^ ]



希望这会有所帮助:)



Tadit

更正了第二个链接'html。

因为它显示了整个html,其中显示了新的窗口图标。

[/编辑]


link 可能会有所帮助。


How do I update a modified row of my datagridview to the db?
i had tried a lot...
but nothing happened....

i asked this question 2 days before....
can any one help me....

tried following code: but...

private void showButton_Click(object sender, EventArgs e)
{
con =  new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMS\\ETMS\\ETMS\\etms.accdb");
String query = "SELECT * FROM Categories";
da = new OleDbDataAdapter(query, con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
da.Update(ds);
}


if i run the code
i am getting the following error
"update requires a valid update command when pressed datarow collection with modified rows"
Please help me...
thank you

解决方案

This may help you.,

private void showButton_Click(object sender, EventArgs e)
{
    con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMS\\ETMS\\ETMS\\etms.accdb");
    String query = "SELECT * FROM Categories";
    da = new OleDbDataAdapter(query, con); 
    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
    ds = new DataSet();
    da.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];
    con.Close();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
    DataSet ds1 = ((DataSet)dataGridView1.DataSource).GetChanges();
    da.Update(ds1);
}


you can do this way:

private void showButton_Click(object sender, EventArgs e)
{
con =  new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMS\\ETMS\\ETMS\\etms.accdb");
String query = "SELECT * FROM Categories";
da = new OleDbDataAdapter(query, con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
dt=new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
da.Update(dt);
}



for further info refer to following links:

Edit Almost Anything in a DataGrid[^]

Auto Saving DataGridView Rows to a SQL Server Database[^]

hope this helps :)

[Edit] by Tadit
Corrected the second link''s html.
As it was showing the whole html where new window icon is shown.
[/Edit]


This link might be of some help.


这篇关于如何将数据网格视图的修改行更新到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆