0

I try to update my DataGrid when i add data into my database

I've tried this : update() refresh() and that : this.produitsTableAdapter.Fill(this.databaseDataSet.produits);.

    private void Bt_ajouter_Click(object sender, EventArgs e)
    {
        string query = @"insert into produits(ref_pdt, designation_pdt, quantite_pdt, prix_pdt)values (@refp, @desig, @quant, @prix)";
        try
        {
            using (var conn = loaddatabaseconnexion.connexion_BDD())
            using (var cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@refp", SqlDbType.NVarChar).Value = tb_ref_add.Text;
                cmd.Parameters.Add("@desig", SqlDbType.NVarChar).Value = tb_des_add.Text;
                cmd.Parameters.Add("@quant", SqlDbType.Int).Value = Int32.Parse(tb_qte_add.Text);
                cmd.Parameters.Add("@prix", SqlDbType.Money).Value = Decimal.Parse(tb_qte_add.Text);


                int rowsAffected = cmd.ExecuteNonQuery();
                if (rowsAffected == 0)
                {
                    MessageBox.Show("Il y a eu un problème !");
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("Données sauvegardées !");
                    formulaire_principal.tableau();

                    conn.Close();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

}

formulaire_principal.tableau(); in this procedure i have that : this.produitsTableAdapter.Fill(this.databaseDataSet.produits);

Arthaiir
  • 23
  • 5

2 Answers2

0

I'm using this prototype if you like it :

//To execute your query
DataSet ds = new DataSet();
SqlDataAdapter da;
da = new SqlDataAdapter(query, conn);
da.Fill(ds, "Produits");

Or you can use your SqlCommand

//To execute your query
DataSet ds = new DataSet();
SqlDataAdapter da;
da = new SqlDataAdapter(cmd);
da.Fill(ds, "Produits");

After that you can use DataSet to Update your DataGridView

//Update your DataGridView
DataGridView1.DataSource = null;
DataGridView1.DataSource = ds.Tables("Produits");
Fatihi Youssef
  • 411
  • 1
  • 6
  • 15
  • thank you , i do that and it is work ! string query = @"select * from produits"; SqlDataAdapter test = new SqlDataAdapter(query, loaddatabaseconnexion.connexion_BDD()); DataSet ds = new DataSet(); dataGridView1.DataSource = null; test.Fill(ds, "produits"); dataGridView1.DataSource = ds.Tables[0]; – Arthaiir Jan 28 '19 at 21:29
0

I don't see where you are currently doing this with the code you have provided, but you are failing to update the data source to your Grid after you call this stored procedure. You can either do this with DataGrid's data-binding capabilities with the ItemsSource attribute or you can do this programatically as well.

Easier Way (IMO): <DataGrid ItemsSource="{Binding Source=YourData}"> <-- You would just update 'YourData' with data from your SP.

I'm not going to re-invent another post, so see this Stack Overflow thread for more information about binding a DataGrid to an object source and make sure you are updating this object with data from your Stored Procedure call. There is a plethora of information on this post of what to do as well as what NOT to do. Also this site should give you a more general idea of data-binding in Windows Forms.

It's hard to give you an exact answer without seeing where you are binding the data to the grid, so this is about as much as I can do given the information included in the question.

Hope it helps. À la prochaine, mon ami.

Scott Mallon
  • 136
  • 1
  • 9