C#:確保綁定到同一資料來源的多個控制項保持同步

來源:互聯網
上載者:User

標籤:datagridview   style   blog   http   color   使用   

下面的程式碼範例示範如何使用 BindingSource 組件,將三個控制項(兩個文字框控制項和一個 DataGridView 控制項)綁定到 DataSet 中的同一列。該樣本示範如何處理BindingComplete 事件,並確保當一個文字框的文本值更改時,會用正確的值更新其他文字框和 DataGridView 控制項。

該樣本使用 BindingSource 來綁定資料來源和控制項。或者,可以直接將控制項綁定到資料來源,並從表單的 BindingContext 檢索用於綁定的 BindingManagerBase,然後為BindingManagerBase 處理 BindingComplete 事件。有關如何進行此操作的樣本,請參見 BindingManagerBase 的 BindingComplete 事件的相關協助頁。

// Declare the controls to be used.private BindingSource bindingSource1;private TextBox textBox1;private TextBox textBox2;private DataGridView dataGridView1;private void InitializeControlsAndDataSource(){    // Initialize the controls and set location, size and     // other basic properties.    this.dataGridView1 = new DataGridView();    this.bindingSource1 = new BindingSource();    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");    // Set the data source to the DataSet.    bindingSource1.DataSource = set1;    //Set the DataMember to the Menu table.    bindingSource1.DataMember = "Menu";    // Add the control data bindings.    dataGridView1.DataSource = bindingSource1;    textBox1.DataBindings.Add("Text", bindingSource1,         "Beverages", true, DataSourceUpdateMode.OnPropertyChanged);    textBox2.DataBindings.Add("Text", bindingSource1,         "Beverages", true, DataSourceUpdateMode.OnPropertyChanged);    bindingSource1.BindingComplete +=         new BindingCompleteEventHandler(bindingSource1_BindingComplete);}private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e){    // Check if the data source has been updated, and that no error has occured.    if (e.BindingCompleteContext ==         BindingCompleteContext.DataSourceUpdate && e.Exception == null)        // If not, end the current edit.        e.Binding.BindingManagerBase.EndCurrentEdit();}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.