开发者

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);
       开发者_JS百科     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...


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


Answered already here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜