我对形式的Load事件填充表收集一个DataGridView,我也有用户填写表单和添加新行后如何更新DataGridView的行列表表onclick事件,我想加入新行到表后更新datagridview的,我使用绑定到一个bindingdatasource一个sqladapter。 这是代码添加新行到表
private void button1_Click(object sender, EventArgs e)
{
connection.Open();
string nv = textBox7.Text.Trim().Replace(',', '.');
//
string sql="";
sql = "INSERT INTO Employe(id_cnss, nom, prenom, adresse, tel,email,mt_heur_travail, titre) VALUES('"+textBox1.Text.ToString().Trim()+"','"+textBox2.Text.ToString().Trim()+"','"+textBox3.Text.ToString().Trim()+"','"+richTextBox1.Text.ToString().Trim()+"','"+textBox4.Text.ToString().Trim()+"','"+textBox5.Text.ToString().Trim()+"',"+nv+",'"+textBox6.Text.Trim()+"')";
// sql = "UPDATE Employe SET id_cnss ='" + + "' , nom ='" + + "', prenom ='" + + "', adresse ='" + + "', tel ='" + + "', email ='" + + "', mt_heur_travail =" + + ", titre ='" + + "' where id_cnss=" + index;
c = new SqlCommand(sql, connection);
c.CommandText = sql;
// IAsyncResult res;
int ex = c.ExecuteNonQuery();
if (ex != null)
{
MessageBox.Show("employé ajouté");
//dataGridView1.Rows.Clear();
//DataTable table = (DataTable)dataGridView1.DataMember;
adapter = new SqlDataAdapter("select * from employe where id_emp=IDENT_CURRENT('EMPLOYE')", connection);
adapter.Fill(dTable);
//adapter.Update(dTable);
dataGridView1.Refresh();
}
connection.Close();
}
该代码表中的所有行添加到DataGridView但我只需要添加添加到数据库中 brievely,当在添加用户点击的最新行,
我想,新的行会被添加到DataGridView 谢谢你提前
目前尚不清楚你的问题是在这里。你问为什么数据库中的新行不显示在datagridview中?或者如何将添加到datagridview的行添加到数据库?还有别的吗? – 2012-04-21 13:20:26
我问为什么数据库的新行(添加onclick事件)不是要么添加到datagridview。 adapter.fill(dtable)添加数据库的所有行,但我只需要添加新行 – tarek 2012-04-21 13:26:08
感谢您的编辑 - 看起来您的问题是,当您使用第二个表单将数据行添加到数据源时,datagridview不会反映这些变化,即使使用.Refresh()命令?我不认为你现在可以发表评论(由于低声誉),所以也许只是更新你的问题。如果这是你的问题,那么很容易解决。 – 2012-04-21 13:27:31