我有一个datagridview,在表单的加载事件中填充了一个表集合,我还有一个由用户填写的表单,并向表onclick事件添加新行,我想在添加新行后更新datagridview表,我使用绑定到绑定数据源的 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 但我只需要简单地添加添加到数据库中的最新行,当用户单击添加时,
我希望将新行添加到 datagridview 提前谢谢你