1

I have a datagridviewComboboxColumn which is binded to a datatable, what i want is when i add

new rows to this datatable from another form the datagridviewComboboxColumn will be updated

in order to choose the added item. i tried to refresh the datasource of the

datagridviewComboboxColumn but i can't get it in a correct way.

EDIT:

This is code i tried (Form2 is the main form containing the datagridview and Categorie

form is the form which i add a new categorie in it):

Form2:

public void refreshDatagrid()
    {
        ((DataGridViewComboBoxColumn)dataGridView2.Columns["idCategorieDataGridViewTextBoxColumn1"]).DataSource = categorieBindingSource;
        ((DataGridViewComboBoxColumn)dataGridView2.Columns["idCategorieDataGridViewTextBoxColumn1"]).DataPropertyName = "IdCategorie";
        ((DataGridViewComboBoxColumn)dataGridView2.Columns["idCategorieDataGridViewTextBoxColumn1"]).DisplayMember = "LibCategorie";
        ((DataGridViewComboBoxColumn)dataGridView2.Columns["idCategorieDataGridViewTextBoxColumn1"]).ValueMember = "IdCategorie";
    }

Categorie form:

public partial class Categorie : Form
{
    Form2 f2;

    public Categorie(Form2 frm2)
    {
        InitializeComponent();
        f2 = frm2;

    }

    private void AjoutCat_Click(object sender, EventArgs e)
    {


        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=|DataDirectory|\database.mdf;Integrated Security=True;User Instance=True";
        con.Open();

        SqlCommand insertCommande = new SqlCommand
        (" INSERT INTO [Categorie] (IdCategorie,LibCategorie)  VALUES (@IdCategorie,@LibCategorie)", con);

        insertCommande.Parameters.AddWithValue("@IdCategorie", CodeCategorietextBox.Text);
        insertCommande.Parameters.AddWithValue("@LibCategorie", LibFamilleTextBox.Text);

        insertCommande.ExecuteNonQuery();

        f2.refreshDatagrid();

        MessageBox.Show("Categorie ajoutée avec succés");
        this.Close();
        con.Close();
    }

}

But when click AjoutCat button in Categorie form and add a new categorie

datagridviewComboboxColumn is not updated.

Can any one show me where the mistake is??

Thanks in advance.

user4374239
  • 93
  • 2
  • 11
  • Have you tried to define a `delegate method` in your `Form2` that updates your `DataGridViewComboboxColumn` values, like shown in this [thread](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c?page=1&tab=votes#tab-top)? – kamino Dec 22 '14 at 10:43

0 Answers0