C # synchronous update of dataGridView textbox

Source: Internet
Author: User
  • . NET Framework 4.5
  • Visual Studio 2010
  • Visual Studio 2008
  • Visual Studio 2005
  • . NET Framework 3.5
  • . NET Framework 2.0
0 (total 2) is helpful for the evaluation of this Article
-Comment on this topic

When using data binding in Windows Forms, multiple controls are often bound to the same data source. In some cases, it may be necessary to take additional steps to ensure that the binding properties of controls are synchronized with each other and synchronized with the data source. In either case, these steps are necessary:

 

  • If the data source is not implemented
    IBindingList, And the generated type
    ItemChanged
    ListChanged event.

  • If the data source is implemented
    Ieditableobject.

You can use
BindingSource binds the data source to the control. In the latter case, useBindingSourceAnd Process
BindingComplete event, and then
BindingManagerBase call
EndCurrentEdit.

Example

The following code example demonstrates how to useBindingSourceWidget, which includes three controls (two text box controls and one

DataGridView Control) bound
The same column in DataSet. This example demonstrates how to handleBindingCompleteEvent, and make sure that when the text value of a text box is changed, other text boxes and
DataGridViewControl.

This example usesBindingSourceTo bind data sources and controls. Alternatively, you can directly bind the control to the data source and

BindingContextBindingManagerBaseAnd thenBindingManagerBaseProcessing

BindingComplete event. For examples of how to perform this operation, seeBindingManagerBaseOf
The Help page for the BindingComplete event.

// Declare the controls to be used.
Private BindingSource bindingSource1;
Private TextBox textBox1;
Private TextBox textBox2;
Private DataGridView maid;

Private void InitializeControlsAndDataSource ()
{
// Initialize the controls and set location, size and
// Other basic properties.
This. Maid = new dview ();
This. bindingSource1 = new BindingSource ();
This. textBox1 = new TextBox ();
This. textBox2 = new TextBox ();
This. dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode. AutoSize;
This. Maid = DockStyle. Top;
This. Maid = 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, performanceupdatemode. OnPropertyChanged );
TextBox2.DataBindings. Add ("Text", bindingSource1,
"Beverages", true, performanceupdatemode. 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. performanceupdate & e. Exception = null)

// If not, end the current edit.
E. Binding. BindingManagerBase. EndCurrentEdit ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.