2010-08-13 68 views
0

我有一个文本菜单控件中的DataGridView,请参见下面的代码片段:如何在ToolStripControlHost中设置DataGridView的DataSource? (C#窗口形式)

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与dataSet1的值。但是,当我单击按钮来更改网格的数据源时,它不会显示dataSet2的记录。 请帮我解决这个问题...谢谢...

+0

代码看起来OK,你可以尝试先设置'dataGridView1.DataSource = null'。也许清除列。 – 2010-08-13 07:51:52

+0

好吧,先生,我会尝试; – yonan2236 2010-08-13 09:27:09

+0

它不工作先生。 – yonan2236 2010-08-13 09:43:01

回答

0

你可能会尝试将DGV的DataSource设置为BindingSource对象,然后修改BindingSource的DataSource。你可以通过调用它的CurrencyManager.Refresh()强制BindingSource更新,如果它不自动更新。

+0

ok先生...但代码应该如何? – yonan2236 2010-08-13 09:37:35

+0

无论你在哪里定义你的DataGridView,也可以坚持在 BindingSource bs = new BindingSource(); dataGridView1.DataSource = bs; 现在,在SetDataSource中,将第一行更改为 bs.DataSource = ds; 您也可能想要在SetDataSource中添加行bs.CurrencyManager.Refresh()。 – Jonathan 2010-08-13 22:09:32

+0

好的..............我会再试一次:) – yonan2236 2010-08-14 01:16:20

相关问题