Search code examples
c#winformstoolstripcontrolhost

How to set the DataSource of a DataGridView inside a ToolStripControlHost? (C# window forms)


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...


Solution

  • Answered already here.