使用C#在Winapp中使用DataGridView [英] DataGridView in winapp using C#

查看:67
本文介绍了使用C#在Winapp中使用DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DataGridView中,我有一些值...

我有一个按钮UPDATE ...

如果我在该gridview中编辑了一些数据...然后单击更新"按钮,则已编辑的数据将存储在数据库中...我该怎么做.

在此先感谢您.

In DataGridView i have some values...

and i have a button UPDATE...

if i edit some data in that gridview...and click that Update button...that edited data is stored in the Database...How can i do it.

Thanks in advance.

推荐答案

您是否正在寻找自动保存功能-然后请参阅 ^ ].

此处 [ ^ ]是DataGridView保存的不错的启动文章.
[
Are you looking for an auto-save feature - then see Auto Saving DataGridView Rows to a SQL Server Database[^].

Here[^] is a decent startup article for DataGridView save.
This[^] may help you as well.


在datagridview中添加隐藏列
在"cellvaluechanged"事件上将其设置为true
现在,您可以通过循环datagridview来仅更新那些行
add a hidden column in datagridview
set its to true on ''cellvaluechanged'' event
now u can update only those rows by looping the datagridview


非常简单:您在datagridview中有一些值,所以现在您创建一个按钮,在按钮内编写以下代码.

for(int i = o; i< datagridview1.rows.count-1; i ++)
{
字符串a = dataGridView1.Rows [i] .Cells ["o_no"].Value.ToString();
字符串b = dataGridView1.Rows [i] .Cells ["c_name"].Value.ToString();
字符串c = dataGridView1.Rows [i] .Cells ["date"].Value.ToString();
字符串d = dataGridView1.Rows [i] .Cells ["kgs"].Value.ToString();

字符串查询="UPDATE yourtablename SET o_no =""+ a +"'',c_name =" + b +'',date =''" + c +'',kgs =""+ d +"其中o_no =""+一个+"";
OleDbConnection con =新的OleDbConnection("yourdatabasepath");
con.Open();
OleDbCommand cmd =新的OleDbCommand(query,con);
cmd.ExecuteNonQuery();
con.Close();
}
messagebox.show(已在数据库上成功编辑");
//现在您已经执行了项目.
very simple: you have some values in datagridview, so now you create one buttons write the following coding within the button.

for(int i=o;i<datagridview1.rows.count-1;i++)
{
string a = dataGridView1.Rows[i].Cells["o_no"].Value.ToString();
string b = dataGridView1.Rows[i].Cells["c_name"].Value.ToString();
string c = dataGridView1.Rows[i].Cells["date"].Value.ToString();
string d = dataGridView1.Rows[i].Cells["kgs"].Value.ToString();

string query = "UPDATE yourtablename SET o_no=''" + a + "'',c_name=''" + b + "'',date=''" + c + "'',kgs=''" + d + "'' where o_no=''" + a + "''";
OleDbConnection con = new OleDbConnection("yourdatabasepath");
con.Open();
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}
messagebox.show("successfully edited on database");
//now you executed your project.


这篇关于使用C#在Winapp中使用DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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