BindingContext.Item[] 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
獲得一個 BindingManagerBase.
多載
| 名稱 | Description |
|---|---|
| Item[Object, String] |
會得到與指定資料來源及資料成員相關聯的 。BindingManagerBase |
| Item[Object] |
取得 BindingManagerBase 與指定資料來源相關的 that。 |
Item[Object, String]
會得到與指定資料來源及資料成員相關聯的 。BindingManagerBase
public:
property System::Windows::Forms::BindingManagerBase ^ default[System::Object ^, System::String ^] { System::Windows::Forms::BindingManagerBase ^ get(System::Object ^ dataSource, System::String ^ dataMember); };
public System.Windows.Forms.BindingManagerBase this[object dataSource, string dataMember] { get; }
public System.Windows.Forms.BindingManagerBase this[object dataSource, string? dataMember] { get; }
member this.Item(obj * string) : System.Windows.Forms.BindingManagerBase
Default Public ReadOnly Property Item(dataSource As Object, dataMember As String) As BindingManagerBase
參數
- dataSource
- Object
與特定 BindingManagerBase相關聯的資料來源。
- dataMember
- String
一條包含可解析為特定 BindingManagerBase的資訊的導航路徑。
屬性值
對於 BindingManagerBase 指定的資料來源和資料成員。
例外狀況
指定 dataMember 內容不存在於資料來源中。
範例
以下程式碼範例示範如何使用 來Item[]取得特定綁定的 。BindingManagerBase 同時也說明如何處理 BindingComplete 事件 BindingManagerBase ,確保綁定於同一資料來源的多個控制值在變動時保持同步。 執行此範例時,將程式碼貼入 Windows 表單,並從表單的建構子或InitializeControlsAndData事件處理方法呼叫該Load方法。
private void InitializeControlsAndData()
{
// Initialize the controls and set location, size and
// other basic properties.
this.dataGridView1 = new DataGridView();
this.textBox1 = new TextBox();
this.textBox2 = new TextBox();
this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = DockStyle.Top;
this.dataGridView1.Location = new Point(0, 0);
this.dataGridView1.Size = new Size(292, 150);
this.textBox1.Location = new Point(132, 156);
this.textBox1.Size = new Size(100, 20);
this.textBox2.Location = new Point(12, 156);
this.textBox2.Size = new Size(100, 20);
this.ClientSize = new Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGridView1);
// Declare the DataSet and add a table and column.
DataSet set1 = new DataSet();
set1.Tables.Add("Menu");
set1.Tables[0].Columns.Add("Beverages");
// Add some rows to the table.
set1.Tables[0].Rows.Add("coffee");
set1.Tables[0].Rows.Add("tea");
set1.Tables[0].Rows.Add("hot chocolate");
set1.Tables[0].Rows.Add("milk");
set1.Tables[0].Rows.Add("orange juice");
// Add the control data bindings.
dataGridView1.DataSource = set1;
dataGridView1.DataMember = "Menu";
textBox1.DataBindings.Add("Text", set1,
"Menu.Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add("Text", set1,
"Menu.Beverages", true, DataSourceUpdateMode.OnPropertyChanged);
BindingManagerBase bmb = this.BindingContext[set1, "Menu"];
bmb.BindingComplete += new BindingCompleteEventHandler(bmb_BindingComplete);
}
private void bmb_BindingComplete(object sender, BindingCompleteEventArgs e)
{
// Check if the data source has been updated, and that no error has occurred.
if (e.BindingCompleteContext ==
BindingCompleteContext.DataSourceUpdate && e.Exception == null)
// If not, end the current edit.
e.Binding.BindingManagerBase.EndCurrentEdit(); ;
}
Dim WithEvents bmb As BindingManagerBase
Private Sub InitializeControlsAndData()
' Initialize the controls and set location, size and
' other basic properties.
Me.dataGridView1 = New DataGridView()
Me.textBox1 = New TextBox()
Me.textBox2 = New TextBox()
Me.dataGridView1.ColumnHeadersHeightSizeMode = _
DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.dataGridView1.Dock = DockStyle.Top
Me.dataGridView1.Location = New Point(0, 0)
Me.dataGridView1.Size = New Size(292, 150)
Me.textBox1.Location = New Point(132, 156)
Me.textBox1.Size = New Size(100, 20)
Me.textBox2.Location = New Point(12, 156)
Me.textBox2.Size = New Size(100, 20)
Me.ClientSize = New Size(292, 266)
Me.Controls.Add(Me.textBox2)
Me.Controls.Add(Me.textBox1)
Me.Controls.Add(Me.dataGridView1)
' Declare the DataSet and add a table and column.
Dim set1 As New DataSet()
set1.Tables.Add("Menu")
set1.Tables(0).Columns.Add("Beverages")
' Add some rows to the table.
set1.Tables(0).Rows.Add("coffee")
set1.Tables(0).Rows.Add("tea")
set1.Tables(0).Rows.Add("hot chocolate")
set1.Tables(0).Rows.Add("milk")
set1.Tables(0).Rows.Add("orange juice")
' Add the control data bindings.
dataGridView1.DataSource = set1
dataGridView1.DataMember = "Menu"
textBox1.DataBindings.Add("Text", set1, "Menu.Beverages", _
True, DataSourceUpdateMode.OnPropertyChanged)
textBox2.DataBindings.Add("Text", set1, "Menu.Beverages", _
True, DataSourceUpdateMode.OnPropertyChanged)
' Get the BindingManagerBase for this binding.
bmb = Me.BindingContext(set1, "Menu")
End Sub
Private Sub bmb_BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs) _
Handles bmb.BindingComplete
' Check if the data source has been updated, and that no error has occurred.
If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate _
AndAlso e.Exception Is Nothing Then
' If not, end the current edit.
e.Binding.BindingManagerBase.EndCurrentEdit()
End If
End Sub
備註
當管理 BindingManagerBase 一組 Binding 資料來源包含多個物件的物件時,使用此過載。 例如,a DataSet 可以包含多個 DataTable 由 DataRelation 物件連結的物件。 在這種情況下,導航路徑是必須的,才能使 返回 BindingContext 正確的 BindingManagerBase。
Note
當參數Item[]有效時,屬性BindingManagerBase總是回傳 一個 dataMember。 它永遠不會回來 null。
請參閱該 Binding 類別以了解可能的資料來源清單,以及關於建立控制項與資料來源綁定的相關資訊。
若目標 BindingManagerBase 管理清單,導航路徑也必須以清單結束。 例如,以下 C# 程式碼將控制 TextBox 項綁定到訂單表中的訂單日期。 導航路徑包含 TableName、 、 RelationName和 ColumnName。 然而,必須 BindingManagerBase 僅 TableName 用 和 RelationName 來檢索(且 且 會解析為一個串列)。
// The navigation path for a Binding ends with a property.
textBox1.DataBindings.Add
("Text", dataSet1, "Customers.custToOrders.OrderDate");
// The navigation path for the BindingManagerBase ends with a list.
BindingManagerBase bmOrders = this.BindingContext
[dataSet1, "Customers.custToOrders"];
回傳 時 BindingManagerBase,你應該使用與 相同的 Binding 資料來源,並只修改導航路徑。
利用該 Contains 方法判斷該目標 BindingManagerBase 是否已存在。
另請參閱
適用於
Item[Object]
取得 BindingManagerBase 與指定資料來源相關的 that。
public:
property System::Windows::Forms::BindingManagerBase ^ default[System::Object ^] { System::Windows::Forms::BindingManagerBase ^ get(System::Object ^ dataSource); };
public System.Windows.Forms.BindingManagerBase this[object dataSource] { get; }
member this.Item(obj) : System.Windows.Forms.BindingManagerBase
Default Public ReadOnly Property Item(dataSource As Object) As BindingManagerBase
參數
- dataSource
- Object
與特定 BindingManagerBase相關聯的資料來源。
屬性值
BindingManagerBase A 代表指定的資料來源。
範例
以下程式碼回傳三個BindingManagerBase物件:一個代表 aDataView,一個代表 ArrayList,以及DataSource屬於控制項的 。BindingTextBox
void ReturnBindingManagerBase()
{
// Get the BindingManagerBase for a DataView.
BindingManagerBase^ bmCustomers = this->BindingContext[ myDataView ];
/* Get the BindingManagerBase for an ArrayList. */
BindingManagerBase^ bmOrders = this->BindingContext[ myArrayList ];
// Get the BindingManagerBase for a TextBox control.
BindingManagerBase^ baseArray = this->BindingContext[ textBox1->DataBindings[ nullptr ]->DataSource ];
}
private void ReturnBindingManagerBase()
{
// Get the BindingManagerBase for a DataView.
BindingManagerBase bmCustomers =
this.BindingContext [myDataView];
/* Get the BindingManagerBase for an ArrayList. */
BindingManagerBase bmOrders =
this.BindingContext[myArrayList];
// Get the BindingManagerBase for a TextBox control.
BindingManagerBase baseArray =
this.BindingContext[textBox1.DataBindings[0].DataSource];
}
Private Sub ReturnBindingManagerBase()
' Get the BindingManagerBase for a DataView.
Dim bmCustomers As BindingManagerBase = _
Me.BindingContext(myDataView)
' Get the BindingManagerBase for an ArrayList.
Dim bmOrders As BindingManagerBase = _
Me.BindingContext(myArrayList)
' Get the BindingManagerBase for a TextBox control.
Dim baseArray As BindingManagerBase = _
Me.BindingContext(Text1.DataBindings(0).DataSource)
End Sub
備註
如果你 BindingManagerBase 想要的路徑不需要導航路徑,就用這個超載。 例如,若管理 BindingManagerBase 一組 Binding 物件 ArrayList ,使用或 DataTable 作為 , DataSource則不需要導航路徑。
Note
該 Item[] 性質總是會回傳一個 BindingManagerBase,且永遠不會返回 null。
請參閱該 Binding 類別以了解可能的資料來源清單,以及關於建立控制項與資料來源綁定的相關資訊。