求高手完善 dataGridView 代码.

rzsgspg 2011-05-07 09:35:01
问题:
1.此代码第一次修改正常.
2.保存后再次修改,就会提示:违反并发性: UpdateCommand 影响了预期 1 条记录中的 0 条

GOOGLE了下大多是说
1.表里设置了自动编号的ID为主键(经过修改尝试--和此处无关)
2.无空值(也没关系)

请教,稍稍改动下代码帮我完善这个GridView吧!

Winform里关键代码

SqlConnection EGG;
SqlDataAdapter adapter;
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“eGG1.Curriculum”中。您可以根据需要移动或移除它。
this.curriculumTableAdapter.Fill(this.eGG1.Curriculum);
EGG = new SqlConnection("server=localhost;database=EGG;uid=sa;pwd=111111");
SqlDataAdapter sda = new SqlDataAdapter("select * from Curriculum", EGG);
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;
}
dataGridView1.Columns[0].ReadOnly = false;
button1.Enabled = true ;
}

private DataTable dbEGG(string strSql)
{
this.EGG.Open();
this.adapter = new SqlDataAdapter(strSql, EGG);
DataTable dtSelect = new DataTable();
int rnt = this.adapter.Fill(dtSelect);
EGG.Close();
return dtSelect;
}

private Boolean dbUpdate()
{
string strSpl = "select * from Curriculum";
DataTable dbUpdate = new DataTable();
dbUpdate = this.dbEGG(strSpl);
dbUpdate.Rows.Clear();
DataTable dtShow = new DataTable();
dtShow = (DataTable)this.dataGridView1.DataSource;
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dbUpdate.ImportRow(dtShow.Rows[i]);
}
try
{
this.EGG.Open();
SqlCommandBuilder CommandBuilder;
CommandBuilder = new SqlCommandBuilder(this.adapter);
this.adapter.Update(dbUpdate);
this.EGG.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}
dbUpdate.AcceptChanges();
return true;
}

private void button3_Click(object sender, EventArgs e)
{
if (dbUpdate())
{
MessageBox.Show("修改成功!");
}

}
...全文
129 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rzsgspg 2011-05-07
  • 打赏
  • 举报
回复
高手都哪去了?
rzsgspg 2011-05-07
  • 打赏
  • 举报
回复
要沉啦,自己顶!
rzsgspg 2011-05-07
  • 打赏
  • 举报
回复
OooOooOooOooO说了大实话
xdf008 2011-05-07
  • 打赏
  • 举报
回复
试试每次对table,row,什么的操作后加上 Accepte()我记得有这个
OooOooOooOooO 2011-05-07
  • 打赏
  • 举报
回复
高手都忙着挣钱去了!!!

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧