如何在 ToolStripControlHost 内设置 DataGridView 的数据源? (C# 窗口窗体)
我在 ContextMenu 控件内有一个 DataGridView,请参阅下面的代码片段:
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
}
这里发生的情况是,当表单打开时,它显示 contextMenu 并在其上显示带有 dataSet1 值的 DataGridView。但是当我单击按钮更改网格的数据源时,它不显示dataSet2的记录。 请帮我解决这个问题...谢谢...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试将 DGV 的 DataSource 设置为 BindingSource 对象,然后修改 BindingSource 的 DataSource。如果 BindingSource 没有自动更新,您可以通过调用其CurrencyManager.Refresh() 来强制它更新。
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.