C # How to bind RadioButton to the DataTable data

Source: Internet
Author: User

The Control class has a public attribute DataBindings, which can bind the Control property with other data. I have access to data binding of the two controls and binding the properties of the controls to the DataTable. The following describes the problems that occur when the DataTable is bound to RadioButton.

The Interface contains two RadioButton, rdoA, and rdoB, as well as a Button that displays dt data. The background defines a private variable DataTable dt. After the interface Initialization is complete, bind the rdo and dt values.Copy codeThe Code is as follows: DataTable dt = null;
Public DataBinding2 ()
{
InitializeComponent ();
Dt = new DataTable ();
Dt. Columns. Add ("A", typeof (bool ));
Dt. Columns. Add ("B", typeof (bool ));
Dt. Rows. Add (rdoA. Checked, rdoB. Checked );
RdoA. DataBindings. Add ("Checked", dt, "");
RdoB. DataBindings. Add ("Checked", dt, "B ");
BtnDTValue. Click + = (a, B) =>
MessageBox. show (string. format ("A: {0} \ r \ nB: {1}", dt. rows [0] ["A"], dt. rows [0] ["B"]);
}

At this point, the Code required to complete the thought seems to have been completed, and you can go to the debugging page to check the results.
It is correct to view dt data before clicking any RadioButton.

Click rdoA to view the dt data again.

An error occurred. After you click rdoA, the checked status of rdoA is true, which is correct, but the rdoA status changes to false. Why is the value of B in the bound dt still true ??
We can carefully study the method of Control. DataBindings. Add.
It has several extension methods. The above example callsCopy codeThe Code is as follows: public Binding Add (string propertyName, object dataSource, string dataMember)

Check what function is actually executed in the background?Copy codeThe Code is as follows: public Binding Add (string propertyName, object dataSource, string dataMember)
{
Return this. Add (propertyName, dataSource, dataMember, false, this. defadatasourdatasourceupdatemode,
Null, string. Empty, null );
}

Continue tracing this. defaultdatasourdatasourceupdatemode. You can see this paragraph in Microsoft's official instructions
"The defaultdatasourdatasourceupdatemode attribute is used when the Binding of performanceupdatemode is not specified to be added to the set. For Windows form controls, this default value is OnValidation ".
The next question is, after clicking rdoA, will the OnValidation function of the rdoB control be executed? So I did a test in another project. (For details about the test method, see here) Two radioButton. Initially, rdoA is true, and rdoB is false. Click rdoB and you will find that the executed events include:
1, rdoA, event: CheckedChanged
2, rdoB, event: CheckedChanged
3, rdoB, event: Validating
4, rdoB, event: Validated
The above results show that the rdoA verification event is not triggered. That is to say, the Checked value of rdoA is not bound to dt at the moment.
The problem lies here. After finding the problem, it is very easy to modify it. Add calls another method of overload and input ceceupdatemode.
RdoA. DataBindings. Add ("Checked", dt, "A", false, DataSourceUpdateMode. OnPropertyChanged );
RdoB. DataBindings. Add ("Checked", dt, "B", false, DataSourceUpdateMode. OnPropertyChanged );

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.