如何添加新行到表后更新的datagridview的行列表 [英] how to update rows list of a datagridview after adding new row to a table

查看:113
本文介绍了如何添加新行到表后更新的datagridview的行列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对形式的Load事件充满了表收集一个DataGridView,我也有填补由用户和添加新行到表onclick事件的一种形式,我想加入新的后更新的DataGridView排表中,我使用绑定到一个bindingdatasource一个sqladapter。 这是code

I have a datagridview filled with a table collection on the load event of the form, i have also a form filled by the user and that add new row to the table onclick event, i would like to update the datagridview after adding new row to the table, i use a sqladapter binded to a bindingdatasource. this is the code

       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();
    }

这code添加表到DataGridView的所有行,但我只需要添加添加到数据库中的最新行 brievely,当用户单击添加下,

this code add all the rows of the table to the datagridview but i need only to add the latest row added to the database brievely, when the user click under add,

我想,新的行会被添加到DataGridView 并感谢你在前进

i would like that the new row will be added to the datagridview And thank you in advance

推荐答案

修改的问题,一个数据表未反映在的DataGridView 是很常见的。问题是,大多数数据表和表适配器实现不提高的ListChanged 事件,是什么的DataGridView 监听到知道什么时候能更新。

The problem of changes to a DataTable not being reflected in a DataGridView is very common. The problem is that most data table and table adapter implementations do not raise ListChanged events which are what the DataGridView listens for to know when to update itself.

.REFRESH()不解决这个问题,因为它只重画客户区,并且不会重新查询数据源 - 底层的DataGridView 不知道任何变化,使它重绘完全一样了。

.Refresh() does not solve the problem because it only redraws the client area, and does not requery the datasource - the underlying DataGridView does not know about any changes so it redraws exactly as before.

这个通常的解决方法是重新设置数据源。

The usual fix for this is to reset the datasource.

dataGridView1.DataSource = null;
// Your datatable goes in the place of newDataSource
dataGridView1.DataSource = newDataSource;

您也可以这样做:

dataGridView1.DataSource = typeof(List);
dataGridView1.DataSource = newDataSource;

这将让任何自动生成列。

This will keep any autogenerated columns.

您可以大概也平滑了这一点,通过使用的BindingSource - 它可能解决不了问题自己,因为绑定源依赖于列表更改的事件也有,但是可能意味着可避免任何奇怪的闪烁在网格中。

You can probably also smooth this out by using a BindingSource - it will probably not solve the problem on its own since the binding source relies on list changed events also, but might mean any strange flicker in the grid can be avoided.

这篇关于如何添加新行到表后更新的datagridview的行列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆