流覽資料 (Windows Forms .NET)
流覽資料來源中記錄的最簡單方式是將元件系結 BindingSource 至資料來源,然後將控制項系結至 BindingSource
。 接著,您可以使用 的 BindingSource
內建導覽方法,例如 MoveNext 、 MoveLast 、 MovePrevious 和 MoveFirst 。 使用這些方法會適當地調整 Position 的 BindingSource
和 Current 屬性。 您也可以藉由設定 Position
屬性來尋找記錄,並將其設定為目前記錄。
若要遞增資料來源中的記錄位置
將 Position 系結資料的 屬性 BindingSource 設定為記錄位置,以移至所需的記錄位置。 下列範例說明當您選取 nextButton
時, MoveNext 使用 的 BindingSource
方法來遞增 Position
屬性。 與 BindingSource
資料集 Northwind
的資料表相關聯 Customers
。
private void nextButton_Click(object sender, System.EventArgs e)
{
this.customersBindingSource.MoveNext();
}
Private Sub nextButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles nextButton.Click
Me.customersBindingSource.MoveNext()
End Sub
注意
將 Position 屬性設定為超出第一筆或最後一筆記錄的值不會產生錯誤,因為 Windows Forms 不會將位置設定為超出清單界限的值。 如果請務必知道您是否已經超過第一筆或最後一筆記錄,請包含邏輯來測試您是否超過資料元素計數。
檢查您是否已超過第一筆或最後一筆記錄
建立 PositionChanged 事件的事件處理常式。 在處理常式中,您可以測試建議的位置值是否已超過實際的資料元素計數。
下列範例說明如何測試是否已到達最後一個資料元素。 在此範例中,如果您位於最後一個專案, 表單上的 [下一步 ] 按鈕會停用。
void customersBindingSource_PositionChanged(object sender, EventArgs e)
{
if (customersBindingSource.Position == customersBindingSource.Count - 1)
nextButton.Enabled = false;
else
nextButton.Enabled = true;
}
Sub customersBindingSource_PositionChanged(ByVal sender As Object,
ByVal e As EventArgs)
If customersBindingSource.Position =
customersBindingSource.Count - 1 Then
nextButton.Enabled = False
Else
nextButton.Enabled = True
End If
End Sub
注意
請注意,如果您變更程式碼中流覽的清單,您應該重新啟用 [ 下一步 ] 按鈕,讓使用者可以流覽新清單的整個長度。 此外,請注意,您 PositionChanged 正在使用的特定 BindingSource 事件必須與其事件處理方法相關聯。
若要尋找記錄,並將它設定為目前專案
尋找您想要設定為目前專案的記錄。 Find如果您的資料來源實作 IBindingList ,請使用 的 方法 BindingSource ,如範例所示。 實 IBindingList
作 的資料來源範例為 BindingList<T> 和 DataView 。
void findButton_Click(object sender, EventArgs e)
{
int foundIndex = customersBindingSource.Find("CustomerID", "ANTON");
customersBindingSource.Position = foundIndex;
}
Sub findButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles findButton.Click
Dim foundIndex As Integer = customersBindingSource.Find("CustomerID",
"ANTON")
customersBindingSource.Position = foundIndex
End Sub
確保子資料工作表中選取的資料列維持在正確的位置
當您在 Windows Forms 中使用資料系結時,您會在父/子或主要/詳細資料檢視中顯示資料。 這是資料系結案例,其中來自相同來源的資料會顯示在兩個控制項中。 在一個控制項中變更選取範圍會造成第二個控制項中顯示的資料也變更。 比方說,第一個控制項可能包含客戶清單,而第二個控制項包含與第一個控制項中所選取客戶相關的訂單清單。
當您在父/子檢視中顯示資料時,可能需要採取額外的步驟,以確保子資料工作表中目前選取的資料列不會重設為數據表的第一個資料列。 若要這樣做,您必須快取子資料工作表位置,並在父資料表變更之後重設它。 一般而言,子資料工作表重設會在父資料表的資料列中第一次變更時發生。
快取目前子資料工作表位置
宣告整數變數來儲存子資料工作表位置,以及用來儲存是否快取子資料工作表位置的布林變數。
private int cachedPosition = -1; private bool cacheChildPosition = true;
Private cachedPosition As Integer = -1 Private cacheChildPosition As Boolean = True
處理繫結 CurrencyManager 的 ListChanged 事件,並檢查 Reset 的 ListChangedType。
檢查 CurrencyManager 的目前位置。 如果大於清單中的第一個專案(通常是 0),請將它儲存至變數。
void relatedCM_ListChanged(object sender, ListChangedEventArgs e) { // Check to see if this is a caching situation. if (cacheChildPosition && cachePositionCheckBox.Checked) { // If so, check to see if it is a reset situation, and the current // position is greater than zero. CurrencyManager relatedCM = sender as CurrencyManager; if (e.ListChangedType == ListChangedType.Reset && relatedCM.Position > 0) // If so, cache the position of the child table. cachedPosition = relatedCM.Position; } }
Private Sub relatedCM_ListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs) ' Check to see if this is a caching situation. If cacheChildPosition AndAlso cachePositionCheckBox.Checked Then ' If so, check to see if it is a reset situation, and the current ' position is greater than zero. Dim relatedCM As CurrencyManager = sender If e.ListChangedType = ListChangedType.Reset _ AndAlso relatedCM.Position > 0 Then ' If so, cache the position of the child table. cachedPosition = relatedCM.Position End If End If End Sub
處理父貨幣管理員的父清單 CurrentChanged 事件。 在處理常式中,設定布林值以指出它不是快取案例。 如果發生
CurrentChanged
,父系的變更是清單位置變更,而不是項目值變更。void bindingSource1_CurrentChanged(object sender, EventArgs e) { // If the CurrentChanged event occurs, this is not a caching // situation. cacheChildPosition = false; }
' Handle the current changed event. This event occurs when ' the current item is changed, but not when a field of the current ' item is changed. Private Sub bindingSource1_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs) Handles bindingSource1.CurrentChanged ' If the CurrentChanged event occurs, this is not a caching ' situation. cacheChildPosition = False End Sub
重設子資料工作表位置
PositionChanged處理子資料工作表系結 的 CurrencyManager 事件。
將子資料表位置重設為儲存在先前程序中的快取位置。
void relatedCM_PositionChanged(object sender, EventArgs e) { // Check to see if this is a caching situation. if (cacheChildPosition && cachePositionCheckBox.Checked) { CurrencyManager relatedCM = sender as CurrencyManager; // If so, check to see if the current position is // not equal to the cached position and the cached // position is not out of bounds. if (relatedCM.Position != cachedPosition && cachedPosition > 0 && cachedPosition < relatedCM.Count) { relatedCM.Position = cachedPosition; cachedPosition = -1; } } }
Private Sub relatedCM_PositionChanged(ByVal sender As Object, ByVal e As EventArgs) ' Check to see if this is a caching situation. If cacheChildPosition AndAlso cachePositionCheckBox.Checked Then Dim relatedCM As CurrencyManager = sender ' If so, check to see if the current position is ' not equal to the cached position and the cached ' position is not out of bounds. If relatedCM.Position <> cachedPosition AndAlso cachedPosition > 0 AndAlso cachedPosition < relatedCM.Count Then relatedCM.Position = cachedPosition cachedPosition = -1 End If End If End Sub
若要測試程式碼範例,請執行下列步驟:
執行範例。
確定已選取 [快 取和重設位置] 核取方塊。
選取 [ 清除父欄位 ] 按鈕,以在父資料表的欄位中造成變更。 請注意,子資料工作表中選取的資料列不會變更。
關閉並重新執行範例。 您必須再次執行它,因為重設行為只會發生在父資料列的第一個變更上。
清除 [快 取和重設位置] 核取方塊。
選取 [ 清除父欄位] 按鈕。 請注意,子資料表中的選取資料列會變更為第一個資料列。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應