C# – how to update rows list of a datagridview after adding new row to a table

cdatagridviewnet

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

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,

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

Best Answer

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

You can also do:

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

This will keep any autogenerated columns.

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.

Related Topic