這裏發生的情況是,當窗體打開時,它顯示contextMenu並使用dataSet1的值在其上顯示DataGridView。但是,當我單擊按鈕來更改網格的數據源時,它不會顯示dataSet2的記錄。問題設置DataGridView的DataSource
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。
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);
當Form 1加載時,彈出窗口contextMenuStrip1和dataGridView1被添加以將其製備dataGridView1在形式消失,dataGridView2僅保留在Form1的項目。
當我打button1
的dataGridView2
從改變其內容基礎公司新增DataSource
(dataSet2
),而dataGridView1仍然顯示的dataSet1
值。 我注意到當時間dataGridView1
被添加到ToolStripHost
,並使其成爲contextMenuStrip1
中的項目時,DataGridView
控件的DataSource
屬性不再被更改。不像dataGridView2
仍然是我沒有添加到contextMenuStrip1
的形式。
謝謝先生,我會盡力的。 – yonan2236 2010-08-14 04:08:47
它的作品!呵呵,我很感激,謝謝......) – yonan2236 2010-08-14 04:10:42
不錯。但是這背後的概念是什麼,即背後的行爲? – TheBlastOne 2011-09-01 08:27:57