求高手完善 dataGridView 代码.
问题:
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("修改成功!");
}
}