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
.