我一直在使用一個使用TabControl
的C#項目。我有一個DataTable
綁定,使用相同的DataView
兩個不同的實體,在一個選項卡中的ListBox
,在另一個選項卡中的DataGridView
。切換標籤重置數據綁定選擇
我已經開始保存用戶對啓動之間的這些控件的選擇。啓動時,ListBox
和DataGridView
都設置爲正確的值。當選項卡開關發生時,如果我沒有更改ListBox
中的選定項目,DataGridView
上的所選行將被設置爲null
,然後調用ListBox
的SelectedIndexChanged
回調函,並認爲列表中的第一項是選擇。但是,如果我強制在啓動時顯示DataGridView
選項卡,則不會發生此問題。
顯示或輸入時,控件和選項卡都沒有任何回調。我在這裏沒有想法了。
示例代碼:
/// <summary>
/// Updates several on-screen controls based on the currently selected item.
/// </summary>
private void updateDisplay()
{
Console.WriteLine("Selection (ListBox): " + myListBox.SelectedIndex);
Console.WriteLine("Selection (DataGridView): " + ((myDataGridView.SelectedRows.Count > 0) ? (""+myDataGridView.SelectedRows[0].Index) : ("null"));
// Update the various labels and textboxes.
}
/// <summary>
/// Callback when the selected index of myListBox changes.
/// </summary>
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
updateDisplay();
}
/// <summary>
/// Callback when the selected row of myDataGridView changes.
/// </summary>
private void myDataGridView_SelectionChanged(object sender, EventArgs e)
{
updateDisplay();
}
結果:
//在推出
選擇(列表框):1種
選擇(DataGridView中):1
//後標籤的變化
選擇(ListBox):0
選擇(DataGridView):null
有什麼想法?
這很有道理。不幸的是,簡單地調用Show()然後在該TabPage上的Hide()並沒有像我希望的那樣創建控件。就目前而言,我只是強調將重點放在該TabPage上,直到我能找出更好的解決方案。 – themarshal 2010-02-03 17:40:14