在dataGridView控件上直接修改数据的问题(用的是论坛上修改过后的代码)
发表在C#图书答疑
2009-03-30
是否精华
是
否
版块置顶:
是
否
我用的是在本论坛上你更正过后的代码:
您好,首先触发DataGridView控件的CellClick事件,然后将Form1.cs中的代码文件替换成如下代码:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test03
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlDataAdapter adapter;
int intindex = 0;
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection("server=.;database=db_16;uid=sa;pwd=");
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_emp",conn);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.RowHeadersVisible = false;
for (int i = 0; i < dataGridView1.ColumnCount;i++ )
{
dataGridView1.Columns[i].Width = 84;
}
button1.Enabled = false;
dataGridView1.Columns[0].ReadOnly = true;
}
private DataTable dbconn(string strSql)
{
if (conn.State == ConnectionState.Open)
conn.Close();
this.adapter = new SqlDataAdapter(strSql, conn);
DataTable dtSelect = new DataTable();
int rnt = this.adapter.Fill(dtSelect);
return dtSelect;
}
private void button2_Click(object sender, EventArgs e)
{
if (dbUpdate())
{
MessageBox.Show("修改成功!");
}
}
private Boolean dbUpdate()
{
string strSql = "select * from tb_emp";
DataTable dtUpdate = new DataTable();
dtUpdate = this.dbconn(strSql);
DataTable dtShow = new DataTable();
dtShow = (DataTable)this.dataGridView1.DataSource;
dtUpdate.ImportRow(dtShow.Rows[intindex]);
SqlCommandBuilder CommandBuiler;
CommandBuiler = new SqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
dtUpdate.AcceptChanges();
return true;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
intindex = e.RowIndex;
}
}
}
但是还会出现一中问题:
比如我要修改第十一行的数据,修改一次成功后,再次修改此行数据就会出现异常:
违反并发性:updateCommand影响了预期1条记录中的0条!
但是要是修改其他行就不会出现问题!
请指教!谢谢!
您好,首先触发DataGridView控件的CellClick事件,然后将Form1.cs中的代码文件替换成如下代码:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test03
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlDataAdapter adapter;
int intindex = 0;
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection("server=.;database=db_16;uid=sa;pwd=");
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_emp",conn);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.RowHeadersVisible = false;
for (int i = 0; i < dataGridView1.ColumnCount;i++ )
{
dataGridView1.Columns[i].Width = 84;
}
button1.Enabled = false;
dataGridView1.Columns[0].ReadOnly = true;
}
private DataTable dbconn(string strSql)
{
if (conn.State == ConnectionState.Open)
conn.Close();
this.adapter = new SqlDataAdapter(strSql, conn);
DataTable dtSelect = new DataTable();
int rnt = this.adapter.Fill(dtSelect);
return dtSelect;
}
private void button2_Click(object sender, EventArgs e)
{
if (dbUpdate())
{
MessageBox.Show("修改成功!");
}
}
private Boolean dbUpdate()
{
string strSql = "select * from tb_emp";
DataTable dtUpdate = new DataTable();
dtUpdate = this.dbconn(strSql);
DataTable dtShow = new DataTable();
dtShow = (DataTable)this.dataGridView1.DataSource;
dtUpdate.ImportRow(dtShow.Rows[intindex]);
SqlCommandBuilder CommandBuiler;
CommandBuiler = new SqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
dtUpdate.AcceptChanges();
return true;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
intindex = e.RowIndex;
}
}
}
但是还会出现一中问题:
比如我要修改第十一行的数据,修改一次成功后,再次修改此行数据就会出现异常:
违反并发性:updateCommand影响了预期1条记录中的0条!
但是要是修改其他行就不会出现问题!
请指教!谢谢!