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

StackOverflow https://stackoverflow.com/questions/3473795

문제

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

도움이 되었습니까?

해결책 2

Answered already here.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top