tags:

views:

73

answers:

2

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.

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
}

I tried adding another DataGridView control (dataGridView2) in my form, but this time I did not put it in a ToolStripControlHost and I did not add it to the contextMenuStrip1.

dataGridView1.DataSource = ds;
dataGridView2.DataSource = ds; // <-- Parent of this is the Form1, the control is not added in the contextMenuStrip.
ToolStripControlHost tsHost = new ToolStripControlHost(dataGridView1);
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add(tsHost);
contextMenuStrip1.Show(textBox1, 0, 27);

When the Form1 loads, the contextMenuStrip1 popups and the dataGridView1 is being added as an item to it making dataGridView1 to disappear in the form and the dataGridView2 only remains in the Form1.

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. What I noticed is when the time dataGridView1 is being added to the ToolStripHost, and make it an Item in the contextMenuStrip1, 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 contextMenuStrip1.

A: 

Just changing the datasource of a control does not tell it to re-bind(refresh) its data from that new datasource. You need to execute the control's DataBind() command after you change its datasource.

So after this:

dataGridView1.DataSource = ds;

try adding this:

dataGridView1.DataBind();
BluMunky
If I'm not mistaken, DataGridView control has no DataBind property. But GridView has in ASP.Net
yonan2236
Oops. Sorry for my mistake Yonan. Glad you got it to work.
BluMunky
+2  A: 

It took me a while, but I found it. Controls in a ToolStripControlHost don't seem to get assigned the BindingContext carried through a regular control tree.

You can take care of this yourself by adding the following to the first line of your SetDataSource method:

dataGridView1.BindingContext = this.BindingContext;

For fair attribution, I got the idea from this web page, where a similar situation was encountered with respect to a ComboBox. I tested it out in a sample app with your code to verify it works.

kbrimington
thanks sir, I will try.
yonan2236
It works! hehe, I'm very grateful, thanks... : )
yonan2236