DataGridView的问题设置数据源 [英] Problem setting DataSource of a DataGridView

查看:192
本文介绍了DataGridView的问题设置数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生的是当窗体打开,它显示的文本菜单,并与dataSet1的值,显示它在DataGridView。但是,当我点击按钮来改变网格的数据源,它不显示dataSet2的记录。

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
}

我尝试添加在我的表格另一个DataGridView控件(dataGridView2),但是这一次我没有把它放在一个ToolStripControlHost 。我不把它添加到contextMenuStrip1

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);

在Form1的负载,contextMenuStrip1弹出窗口和dataGridView1被添加作为一个项目来将其制作dataGridView1在表单中消失,dataGridView2不仅仍然在Form1。

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.

当我打的按钮1 dataGridView2 更改其内容基地的新的数据源 dataSet2 ),而dataGridView1仍显示 dataSet1的的价值。
我注意到的是,当时间 dataGridView1 被添加到 ToolStripHost ,并使其成为项目在 contextMenuStrip1 DataGridView的控制数据源属性不被再改变。不同于 dataGridView2 剩下的形式,我没有添加到 contextMenuStrip1

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.

推荐答案

我花了一段时间,但我发现它。在 ToolStripControlHost 似乎并不控制得到分配的BindingContext 过有规律的控件树进行。

It took me a while, but I found it. Controls in a ToolStripControlHost don't seem to get assigned the BindingContext carried through a regular control tree.

您可以通过添加以下到你的的setDataSource 方法的第一行利用这个照顾自己
$ b

You can take care of this yourself by adding the following to the first line of your SetDataSource method:

dataGridView1.BindingContext = this.BindingContext;

有关公平的归属,我接到的此网页,在类似的情况是相对于组合框遇到。我用你的代码,以验证它的工作原理测试了它在一个示例应用程序。

For fair attribution, I got the idea from this web page, where a similar situation was encountered with respect to a ComboBox. I tested it out in a sample app with your code to verify it works.

这篇关于DataGridView的问题设置数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆