views:

27

answers:

1

I have a DataGridView inside a ContextMenu control, please see the code snippet below:

private void Form1_Load(object sender, EventArgs e)
        {
            SetDataSource(dataSet1);// A populated DataSet
        }

protected void SetDataSource(DataSet ds)
        {
            dataGridView1.DataSource = ds;
            ToolStripControlHost tsHost = new ToolStripControlHost(dataGridView1);
            contextMenuStrip1.Items.Clear();
            contextMenuStrip1.Items.Add(tsHost);
            contextMenuStrip1.Show(textBox1, 0, 27);
        }

 private void button1_Click(object sender, EventArgs e)
        {
            SetDataSource(dataSet2);// Another populated DataSet
        }

What happens here is when in the form opens, it shows the contextMenu and display the DataGridView on it with the value of dataSet1. But when I click the button to change the DataSource of the Grid, It doesn't show the records of dataSet2. Please help me how to fix this... thanks...

A: 

You might try setting the DGV's DataSource to a BindingSource object, and then modifying the BindingSource's DataSource instead. You can force the BindingSource to update, if it doesn't automatically, by invoking its CurrencyManager.Refresh().

Jonathan
ok sir... but how should the code will look like?
yonan2236
Wherever you define your DataGridView, also stick in BindingSource bs = new BindingSource(); dataGridView1.DataSource = bs;Now, in SetDataSource, change the first line to bs.DataSource = ds;You may also want to add the line bs.CurrencyManager.Refresh() in SetDataSource as well.
Jonathan
ok.............. I'll try again : )
yonan2236
still, it doesn't work :(
yonan2236
I tried to add another DataGridViewControl in my form (dataGridView2), then set the DataSource of dataGridView2 as the same of dataGridView1. The only difference is I did not add the dataGridView2 to the ToolStripControlHost, it is only displayed in the form.
yonan2236
Initially, this two DataGridViews are displayed in a form. when the form loads, the dataGridView1 is being added to the contextMenu causing it to disappear in the form and leaving the other DataGridView (dataGridView2). When I hit the button1 the dataGridView2 changes its content base from its new DataSource (dataSet2), while the dataGridView1 still displays the value of dataSet1.
yonan2236
What I noticed is when the time dataGridView1 is being added to the ToolStripHost, and make it an Item in the controltextMenu, the DataSource property of the DataGridView control is not being changed anymore. Unlike the dataGridView2 that remains in the form which I did not add to the controltextMenu.
yonan2236