DataGirdView编辑问题 DataGridView的一行包括许多单元格其中里面可以编辑数据怎样让焦点一离开这一行的时候,自动更新数据到数据库?最好能提供代码!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) { string connstr = "user id=manager;password=foton;data source=ecudb"; using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connstr)) { DataSet ds = new DataSet(); string selectSql = "update table1 set t_orderinfo.goods='商品名称' where ...条件(根据你gridview上边某个唯一的条件判断)"; System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(selectSql, conn); cmd.ExecuteNonQuery(); } } 离开这一行有点麻烦。如果只是编辑完了就要更新数据的话,那可以用CellEndEdit事件,在其中连接数据库操作。 这样就可以了: private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e) { string connstr = "user id=manager;password=foton;data source=ecudb"; using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connstr)) { DataSet ds = new DataSet(); string selectSql = "update table1 set t_orderinfo.goods='商品名称' where ...条件(根据你gridview上边某个唯一的条件判断)"; System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(selectSql, conn); cmd.ExecuteNonQuery(); } } 注册RowLeave事件。dataGridView1.RowLeave += new DataGridViewCellEventHandler(dataGridView1_RowLeave);void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e) { if (this.dataGridView1.IsCurrentRowDirty) { this.dataGridView1.EndEdit(); Save(e.RowIndex); } }private void Save(int rowIndex) {......} 搂主需要给每行加个标志位,如果这一行被修改了,那么设置该行的标志位某一数值比如True然后,当用户选择其它行时,判断这个标志位,如果为True那么更新到数据库,这样是比较常用的做法标志位搂主可以用DataGridViewRow类的Tag属性这样做的好处是,用户修改完该行的所有单元格时才会提交到数据库;否则如果每修改一个单元格就提交的话,有可能数据不完整,数据访问也太频繁 就是下面代码的意思,搂主参考一下:private DataGridViewRow m_PrevSelectedRow = null;//SelectionChanged事件是行之间切换选中的时候触发的private void dataGridView1_SelectionChanged(object sender, EventArgs e){ if (this.m_PrevSelectedRow != null && this.m_PrevSelectedRow.Tag != null) { if (this.m_PrevSelectedRow.Tag.Equals(true)) { //在这里,开始更新你的数据表 } } this.m_PrevSelectedRow = this.dataGridView1.CurrentRow;} 哦,还少一个事件代码,就是在单元格改变的时候,修改标志位:private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){ DataGridViewRow currRow = this.dataGridView1.Rows[e.RowIndex]; DataGridViewCell currCell = currRow.Cells[e.ColumnIndex]; if (currRow.Tag == null || !currRow.Tag.Equals(true)) currRow.Tag = true;} 哦,还少一个事件代码,就是在单元格改变的时候,修改标志位:private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){ DataGridViewRow currRow = this.dataGridView1.Rows[e.RowIndex]; DataGridViewCell currCell = currRow.Cells[e.ColumnIndex]; if (currRow.Tag == null || !currRow.Tag.Equals(true)) currRow.Tag = true;} if (this.m_PrevSelectedRow.Tag.Equals(true)) { //在这里,开始更新你的数据表 注意:这里是把行:this.m_PrevSelectedRow里面的各单元格的数据提取出来,然后提交到数据库提取就不用多说了:单元格的值=this.m_PrevSelectedRow.Cells[index].Value; 关于字符串乱码的问题 SiteMapPath怎么样动态的添加title和url ExecuteReader: CommandText 属性尚未初始化 怎么将有规则的XML字符串转换成二维数组 问一个Winform的问题请大虾帮忙啦谢谢! C#问题:Buff += strIn; 抽屉菜单 Win98下自定义纸张大小单位不明确的问题?(全部可用分送上) [提问] C#如何获取IIS framework runtime version? 如何实现计数,如下面这段代码,如何用C#来实现 请教各位像这样的网站做一个要多少钱? 一个serialport串口控件的问题
{
string connstr = "user id=manager;password=foton;data source=ecudb";
using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connstr))
{
DataSet ds = new DataSet();
string selectSql = "update table1 set t_orderinfo.goods='商品名称' where ...条件(根据你gridview上边某个唯一的条件判断)";
System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(selectSql, conn);
cmd.ExecuteNonQuery();
}
}
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
string connstr = "user id=manager;password=foton;data source=ecudb";
using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connstr))
{
DataSet ds = new DataSet();
string selectSql = "update table1 set t_orderinfo.goods='商品名称' where ...条件(根据你gridview上边某个唯一的条件判断)";
System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(selectSql, conn);
cmd.ExecuteNonQuery();
}
}
dataGridView1.RowLeave += new DataGridViewCellEventHandler(dataGridView1_RowLeave);
void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.IsCurrentRowDirty)
{
this.dataGridView1.EndEdit();
Save(e.RowIndex);
} }
private void Save(int rowIndex)
{......}
然后,当用户选择其它行时,判断这个标志位,如果为True那么更新到数据库,这样是比较常用的做法标志位搂主可以用DataGridViewRow类的Tag属性这样做的好处是,用户修改完该行的所有单元格时才会提交到数据库;
否则如果每修改一个单元格就提交的话,有可能数据不完整,数据访问也太频繁
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (this.m_PrevSelectedRow != null && this.m_PrevSelectedRow.Tag != null)
{
if (this.m_PrevSelectedRow.Tag.Equals(true))
{
//在这里,开始更新你的数据表 }
}
this.m_PrevSelectedRow = this.dataGridView1.CurrentRow;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow currRow = this.dataGridView1.Rows[e.RowIndex];
DataGridViewCell currCell = currRow.Cells[e.ColumnIndex];
if (currRow.Tag == null || !currRow.Tag.Equals(true))
currRow.Tag = true;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow currRow = this.dataGridView1.Rows[e.RowIndex];
DataGridViewCell currCell = currRow.Cells[e.ColumnIndex];
if (currRow.Tag == null || !currRow.Tag.Equals(true))
currRow.Tag = true;
}
{
//在这里,开始更新你的数据表 注意:这里是把行:this.m_PrevSelectedRow里面的各单元格的数据提取出来,然后提交到数据库
提取就不用多说了:
单元格的值=this.m_PrevSelectedRow.Cells[index].Value;