如何实现gridview的更新,删除,而不直接是相对于数据库的操作 我现在就是要实现将我gridview中显示出来的数据,更新,删除,但是不是直接相对于数据库操作的,最后一步是把我显示的数据经过我的更新,删除后,才保存到数据库中的。请贴出实际的代码。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主的意思是 自己实现对gridview中的数据 进行更新和删除操作吗?还是说 在dridview 中 编辑数据时 并不会更新和删除数据库的数据, 直到点保存后才保存到数据库? 回复2楼自己实现对gridview中的数据 进行更新和删除操作在dridview 中 编辑数据时 并不会更新和删除数据库的数据, 直到点保存后才保存到数据库 楼上的哥哥门:我的意思难道还不明白吗? 什么一个保存按扭,你们不要再搞笑了,如果不会 就不要乱说撒。 我重新说明:比如我的gridview中有20条数据,现在我要对这20条数据删除,修改,申明这20条数据是没有插入到库中的,我如何更新呢~ 最后 我才保存到数据库中,明白?各位大仙, 利用SqlCommandBuilder实现批量更新SqlCommandBuilder:自动生成单表命令,用于将对 DataSet所做的更改与关联的 SQL Server 数据库的更改相协调。什么时候用SqlCommandBuilder?a. 有时候需要缓存的时候,比如说在一个商品选择界面,选择好商品,并且进行编辑/删除/更新后,最后一并交给数据库,而不是每一步操作都访问数据库,因为客户选择商品可能进行N次编辑/删除更新操作,如果每次都提交,不但容易引起数据库冲突,引发错误,而且当数据量很大时在用户执行效率上也变得有些慢b.有些情况下只往数据库里更新,不读取. 也就是说没有从数据库里读,SqlDataAdapter也就不知道是更新哪张表了,调用Update就很可能出错了。这样的情况下可以用SqlCommandBuilder 了.注意点:1.只能更新一个表,如果此数据集是从两个或者两个以上的表关联而来的,则不能用此方法自动更新2.表中必须设置主键3.更新的表中字段不能有image类型的4.它是一个密封类优点:节省代码量,节省时间,这个方法可以代替所有的: 更新/删除/插入操作语句缺点:访问两次数据库,效率稍微有些慢具体实现 public partial class Form1 : Form { public Form1() { InitializeComponent(); } SqlDataAdapter da; DataTable dt; private void Form1_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection("server=.;database=pubs;integrated security=true"); da = new SqlDataAdapter("select * from jobs", con); dt = new DataTable(); da.Fill(dt); this.dataGridView1.DataSource = dt; } private void button1_Click(object sender, EventArgs e) { SqlCommandBuilder scb = new SqlCommandBuilder(da); if (da.Update(dt) > 0) { MessageBox.Show("批量更新成功"); } else { MessageBox.Show("批量更新失败"); } } } 先对数据源dataset更新,修改最后都结束了,再把结果提交,对吧?LZ是这意思不?最后这样不就行了? dataAdapter.update(dataset) 你操作之后不用datatable去更新数据库不就行了 我重新说明:比如我的gridview中有20条数据,现在我要对这20条数据删除,修改,申明这20条数据是没有插入到库中的,我如何更新呢~ 最后 我才保存到数据库中,明白?各位大仙, 定义泛型集合List,先将数据保存到List集合中。没连库之前直接操作List中的数据。对其进行添、删、改、查。最后用事务将List中的数据一并添加到数据库。 DataTable dt="20条数据"然后你只需要对这个表操作都可以了。如你可以对这个表里的一行进行删除,,然后你在datagridview1.datasource=dt;,,, 难道不对吗? 之前随便你怎么操作datatable list<> 最后更新啊 建立一个临时的datatable,所有的操作均在datatable中执行,操作时对不同操作分类记录,最后确定保存时,循环读取datarow中数据更新数据库应该就好了 实体类 class Emplyee { public Emplyee(){ } public Emplyee(int id, String dept, String name, double sal) { this.id = id; this.dept = dept; this.name = name; this.sal = sal; } private int id; public int Id { get { return id; } set { id = value; } } ...... }具体实现 //定义泛型集合 Dictionary<int, Emplyee> dic = new Dictionary<int, Emplyee>(); ...... //添加 try { int id = Convert.ToInt32(this.txtID.Text); String dept = this.txtDept.Text.ToString(); String name = this.txtName.Text.ToString(); double sal = Convert.ToDouble(this.txtSal.Text); Emplyee emp = new Emplyee(id, dept, name, sal); dic.Add(id, emp); ...... } catch (Exception ex) { MessageBox.Show(ex.Message); } finally{ } ...... //编辑 try { int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["id"].Value); dic[id].Id = Convert.ToInt32(this.txtID.Text); dic[id].Dept = this.txtDept.Text.ToString(); dic[id].Name = this.txtName.Text.ToString(); dic[id].Sal = Convert.ToDouble(this.txtSal.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally{ } ...... //dataGridView1_CellClick this.txtID.Text = this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString(); this.txtDept.Text = this.dataGridView1["dept", this.dataGridView1.SelectedRows[0].Index].Value.ToString(); this.txtName.Text = this.dataGridView1["name", this.dataGridView1.SelectedRows[0].Index].Value.ToString(); this.txtSal.Text = this.dataGridView1["sal", this.dataGridView1.SelectedRows[0].Index].Value.ToString(); ...... //提交事务 SqlConnection con = new SqlConnection("server=.;database=pubs;integrated security=true"); SqlCommand com = con.CreateCommand(); con.Open(); SqlTransaction tran = con.BeginTransaction(); com.Transaction = tran; try { for (int i = 0; i < this.dataGridView1.RowCount; i++) { com.CommandText = string.Format("insert into Emplyee values({0},'{1}','{2}','{3}')", ...... ); com.ExecuteNonQuery(); } ...... tran.Commit(); MessageBox.Show("事务提交成功"); } catch (Exception ex) { MessageBox.Show(ex.Message); tran.Rollback(); MessageBox.Show("事务提交失败"); } finally { con.Close(); } 感谢h_ls07你编辑的时候是以外部的textbox的更新 然后在更新到gridview中的把, 这里我要的就是直接更新gridview中的数据,怎么做到呢? 用update()这个方法和泛型一起用,你看看行吗?以前在公司好像用的过的,你试试吧!!不对的地方还请多多指教啊!!! 1、更新dataGridView中数据后,遍历dataGridView,判断哪些数据被修改过。然后通过键值对去修改集合中的数据。2、用Update()批量更新,但是这种方法是自动生成单表命令。sql语句不够简练。 你还是不明白我的意思呀看你的代码://编辑 try { int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["id"].Value); dic[id].Id = Convert.ToInt32(this.txtID.Text); dic[id].Dept = this.txtDept.Text.ToString(); dic[id].Name = this.txtName.Text.ToString(); dic[id].Sal = Convert.ToDouble(this.txtSal.Text); }你是在gridview外部更新,把外部的textbox值 更新为内部gridview中的值,这样当然没有问题,现在我的是没有那些你所谓的this.txtDept.Text.ToString();怎么更新难道:dic[id].Dept=dic[id].Dept???????? dic[id].Dept=this.dataGridView1.SelectedRows[0].Cells["dept"].Value.ToString();ordic[id].Dept=this.dataGridView1["dept", this.dataGridView1.SelectedRows[0].Index].Value.ToString(); h_ls07非常感谢,现在我要实现的是这样的,就是我每次是以外部的excel文件作为数据源,然后把excel中的数据显示在gridview中,没有保存到数据库中的,(已经解决),现在就是要对 gridview中的数据进行更新,删除,最后保存到数据库中。 比如excel中的数据字段为:start_code, end_code 导入gridview中显示出来,现在要对导入到gridview中的 start_code和 end_code进行更新删除。 泛型从来没有用过,请你那我提供的资料信息做个例子,马上结帖给分,多谢多谢,其实小弟很菜~ 用一个保存按钮可以实现这个功能,dataAdapter.Update(datSet)。但是这样的SQL语句太浪费资源,一般在实际项目开发中不会采取这种方法。我们都是自己写SQL语句。你上面说的,可以用集合来做,也可以自己定义DataTable来实现既然说到集合,我就用集合做了一个例子。下载 h_ls07 你给的只是一个导入数据显示数据的过程,我真正要的是更新,删除数据的过程,其实你上面给的代码不错,我的需求是这样的,我每次导入一个 excel就可以把excel中的数据显示在gridview中,并且我可以更新,删除我所导入的数据,最后我确定没有问题了就保存到数据库中。 很感谢你 可以达到我的这个需求么 你直接在dataGridView中添加、删除、修改。完了之后再保存,提交到数据库啊 。Smother。。 楼上的说的不少了我也写个思路吧.你dgv绑定的是数据源.你可以直接对数据源操作.也就是说你在每一行dgv里进行操作时,在退出行时进行内存表的修改.(根据绑定的行ID修改数据源对应的ID行)最后通过数据适配器adapter的update进行数据库里表的更新. 将数据源从数据库出取出来时,存入集合中,将这个集合做为gridview的数据源,所有增删改都在List集合中操作,最后点击保存按钮时,再把这个List集合写入数据库就好了。 h_ls07 说的已经很明白了,LZ还不懂? 求助一道C#编程题 技术确认的问题,大神求教 求C#操作xml文件问题 小弟新手学触发器!小问题请高手指点 C# 可否静态链接C 语言生成的Lib 库? c#如何截获WebBrowser的文件请求? 请教关于编码问题 最近在学习WPF,哈哈哈哈哈 webclient设置代理问题,好像怎么设置都不行 help,初学者 axw winform动态创建rdlc报表文件,动态绑定数据集
还是说 在dridview 中 编辑数据时 并不会更新和删除数据库的数据, 直到点保存后才保存到数据库?
自己实现对gridview中的数据 进行更新和删除操作
在dridview 中 编辑数据时 并不会更新和删除数据库的数据, 直到点保存后才保存到数据库
什么一个保存按扭,你们不要再搞笑了,如果不会 就不要乱说撒。
我重新说明:比如我的gridview中有20条数据,现在我要对这20条数据删除,修改,申明这20条数据是没有插入到库中的,我如何更新呢~
最后 我才保存到数据库中,明白?各位大仙,
a. 有时候需要缓存的时候,比如说在一个商品选择界面,选择好商品,并且进行编辑/删除/更新后,最后一并交给数据库,而不是每一步操作都访问数据库,因为客户选择商品可能进行N次编辑/删除更新操作,如果每次都提交,不但容易引起数据库冲突,引发错误,而且当数据量很大时在用户执行效率上也变得有些慢
b.有些情况下只往数据库里更新,不读取. 也就是说没有从数据库里读,SqlDataAdapter也就不知道是更新哪张表了,调用Update就很可能出错了。这样的情况下可以用SqlCommandBuilder 了.注意点:
1.只能更新一个表,如果此数据集是从两个或者两个以上的表关联而来的,则不能用此方法自动更新
2.表中必须设置主键
3.更新的表中字段不能有image类型的
4.它是一个密封类优点:
节省代码量,节省时间,这个方法可以代替所有的: 更新/删除/插入操作语句缺点:
访问两次数据库,效率稍微有些慢具体实现 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} SqlDataAdapter da;
DataTable dt; private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=pubs;integrated security=true");
da = new SqlDataAdapter("select * from jobs", con);
dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt;
} private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(da); if (da.Update(dt) > 0)
{
MessageBox.Show("批量更新成功");
}
else
{
MessageBox.Show("批量更新失败");
}
}
}
最后这样不就行了? dataAdapter.update(dataset)
最后 我才保存到数据库中,明白?各位大仙,
难道不对吗? 之前随便你怎么操作datatable list<> 最后更新啊
实体类 class Emplyee
{
public Emplyee(){ }
public Emplyee(int id, String dept, String name, double sal)
{
this.id = id;
this.dept = dept;
this.name = name;
this.sal = sal;
}
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
......
}具体实现 //定义泛型集合
Dictionary<int, Emplyee> dic = new Dictionary<int, Emplyee>();
......
//添加
try
{
int id = Convert.ToInt32(this.txtID.Text);
String dept = this.txtDept.Text.ToString();
String name = this.txtName.Text.ToString();
double sal = Convert.ToDouble(this.txtSal.Text);
Emplyee emp = new Emplyee(id, dept, name, sal);
dic.Add(id, emp);
......
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally{ }
......
//编辑
try
{
int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["id"].Value);
dic[id].Id = Convert.ToInt32(this.txtID.Text);
dic[id].Dept = this.txtDept.Text.ToString();
dic[id].Name = this.txtName.Text.ToString();
dic[id].Sal = Convert.ToDouble(this.txtSal.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally{ }
......
//dataGridView1_CellClick
this.txtID.Text = this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();
this.txtDept.Text = this.dataGridView1["dept", this.dataGridView1.SelectedRows[0].Index].Value.ToString();
this.txtName.Text = this.dataGridView1["name", this.dataGridView1.SelectedRows[0].Index].Value.ToString();
this.txtSal.Text = this.dataGridView1["sal", this.dataGridView1.SelectedRows[0].Index].Value.ToString();
......
//提交事务
SqlConnection con = new SqlConnection("server=.;database=pubs;integrated security=true");
SqlCommand com = con.CreateCommand();
con.Open();
SqlTransaction tran = con.BeginTransaction();
com.Transaction = tran;
try
{
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
com.CommandText = string.Format("insert into Emplyee values({0},'{1}','{2}','{3}')",
......
);
com.ExecuteNonQuery();
}
......
tran.Commit();
MessageBox.Show("事务提交成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
tran.Rollback();
MessageBox.Show("事务提交失败");
}
finally
{
con.Close();
}
你编辑的时候是以外部的textbox的更新 然后在更新到gridview中的把,
这里我要的就是直接更新gridview中的数据,怎么做到呢?
2、用Update()批量更新,但是这种方法是自动生成单表命令。sql语句不够简练。
你还是不明白我的意思呀看你的代码
://编辑
try
{
int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["id"].Value);
dic[id].Id = Convert.ToInt32(this.txtID.Text);
dic[id].Dept = this.txtDept.Text.ToString();
dic[id].Name = this.txtName.Text.ToString();
dic[id].Sal = Convert.ToDouble(this.txtSal.Text);
}
你是在gridview外部更新,把外部的textbox值 更新为内部gridview中的值,这样当然没有问题,
现在我的是没有那些你所谓的this.txtDept.Text.ToString();怎么更新
难道:dic[id].Dept=dic[id].Dept????????
dic[id].Dept=this.dataGridView1.SelectedRows[0].Cells["dept"].Value.ToString();
or
dic[id].Dept=this.dataGridView1["dept", this.dataGridView1.SelectedRows[0].Index].Value.ToString();
非常感谢,现在我要实现的是这样的,就是我每次是以外部的excel文件作为数据源,然后把excel中的数据显示在gridview中,没有保存到数据库中的,(已经解决),现在就是要对 gridview中的数据进行更新,删除,最后保存到数据库中。 比如excel中的数据字段为:start_code, end_code 导入gridview中显示出来,现在要对导入到gridview中的 start_code和 end_code进行更新删除。
泛型从来没有用过,请你那我提供的资料信息做个例子,马上结帖给分,多谢多谢,其实小弟很菜~
但是这样的SQL语句太浪费资源,一般在实际项目开发中不会采取这种方法。我们都是自己写SQL语句。
你上面说的,可以用集合来做,也可以自己定义DataTable来实现
既然说到集合,我就用集合做了一个例子。
下载
你给的只是一个导入数据显示数据的过程,我真正要的是更新,删除数据的过程,其实你上面给的代码不错,我的需求是这样的,我每次导入一个 excel就可以把excel中的数据显示在gridview中,并且我可以更新,删除我所导入的数据,最后我确定没有问题了就保存到数据库中。
很感谢你
可以达到我的这个需求么
。Smother。。
你可以直接对数据源操作.也就是说你在每一行dgv里进行操作时,在退出行时进行内存表的修改.(根据绑定的行ID修改数据源对应的ID行)最后通过数据适配器adapter的update进行数据库里表的更新.