2015-04-04 27 views
0

在我的WinForms應用程序,我已經三個選項卡,並在每個選項卡gatagridview。 如果我點擊一個BTN在TAB1的datagridview顯示的記錄,它填充如果我去TAB2,並觸及按鈕來顯示不同的記錄,它首先顯示我正確的記錄,然後dispalys我同樣的記錄TAB1或如果點擊不同的按鈕,則爲tab3。如何正確獲取datagridview的

然後,它提醒我從TAB1,TAB2 TAB3和相同的記錄。

我怎麼能解決這個問題,或者是它,我宣佈globale的DataTable dt的變量?

+1

我們需要查看代碼的相關部分。 – Marco 2015-04-04 11:57:23

回答

1

如果您希望3 DGVs擁有各自的記錄指針,則不能直接使用DataTable作爲DataSource。相反,每個人都使用中間的BindingSource

// assume a few DataGridViews..: 
    DataGridView DGV1 = new DataGridView(); 
    DataGridView DGV2 = new DataGridView(); 
    DataGridView DGV3 = new DataGridView(); 

    // and a common DataTable: 
    DataTable DT = new DataTable(); 
    //.. 

    // we need a separate BindingSource for each DGV: 
    BindingSource BS1 = new BindingSource(); 
    BindingSource BS2 = new BindingSource(); 
    BindingSource BS3 = new BindingSource(); 

    // each is bound to the DataTable 
    BS1.DataSource = DT; 
    BS2.DataSource = DT; 
    BS3.DataSource = DT; 

    // now we set them to be the DatSource of the DGVs: 
    DGV1.DataSource = BS1; 
    DGV2.DataSource = BS2; 
    DGV3.DataSource = BS3; 

    // now we can set the record pointers separately: 
    BS1.Position = 3;   
    BS2.Position = 0;   
    BS3.Position = BS3.Count - 1; 

    // or set filters: 
    BS2.Filter = "someCondition"; 

    // or set sorts: 
    BS3.Filter = "someSort"; 
+0

@ TaW 非常感謝。一個問題,但如果我有兩個DGV,我該如何設置他們的位置? 我可以這樣嗎: 'BS4.Position = 2 BS5.Position = 1' 沒有count-1? – mikybrain 2015-04-04 17:11:05

+0

是的,你可以創建任意數量BindingSources'的'以任何方式你need..Just使他們成爲其他DGVs的'DataSources',應控制好每一個。該'BS3.Position = BS3.Count - 1;'只是如何,您可以顯示__last__記錄的例子'(計數 - 1)' – TaW 2015-04-04 17:15:18

+0

日Thnx非常多。我不知道用'BindingSources'。祝你美好前夕&復活節 – mikybrain 2015-04-04 17:20:27