How to bind. NET data list controls (WebForm, Winform, and WPF)

Source: Internet
Author: User

WebForm:

Data list control:

The list binding controls in WebForm are basically GridView, DataList, and Repeater. Of course, there are other DropDownList and ListBox controls.


Their common data source setting methods:

XXX. DataSource = data source.


What are the requirements for the data source format? The simplest way is to give it a try and wait for it to throw an exception:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2011503N5-0.jpg "style =" line-height: 1.5; "/>


As shown in the preceding error, three data source binding methods are supported: IListSource, IEnumerable, and IDataSource.


About Winform

Data list control:

The list binding controls in WinForm are usually DataGridView, ListView, and ComboBox.


Their common data source setting methods:

XXX. DataSource = data source.


In the same way, it is found that it does not throw an exception, you can view the code of the DataGridView DataSource:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2011503345-1.jpg "/>

Let's take a look at the DataSource of ComboBox:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/20115035U-2.jpg "style =" line-height: 1.5; "/>

You can obtain information from the preceding prompt. The data source supports the IListSource interface.


Look at WPF

Data list control:

The list binding controls in WPF are DataGrid, ListBox, and ComboBox.


Their common data source setting methods:

XXX. ItemsSource = data source.


For WPF, its ItemsSource is no longer of the Object type, but IEnumerable:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2011501492-3.jpg "/>

You can obtain information from the preceding prompt. The data source supports the IEnumerable interface.


Comparison:

WebForm: IListSource, IEnumerable, IDataSource.

Winform: IListSource

WPF: IEnumerable


Commonalities:

The IListSource interface must return IList, And the IList interface also inherits from the IEnumerable interface.

Therefore, the ultimate commonality is the IEnumerable interface. Generally speaking, all data sources such as List <T> are supported.


Differences: use generic sets such as IDataReader, DataTable, and DataSet)

WebForm: supports IDataReader, able, and DataSet.

Winform: IDataReader is not supported, and DataTable and DataSet are supported.

WPF: IDataReader is supported, and DataTable and DataSet are not supported.


There are some stories:


Winform: Is IDataReader not supported for Xiami?

For the DataGridView list control, the table can be directly modified, and the modified value is directly returned to the bound data source.

Due to this nature, read-only IDataReader is not suitable for read/write dual requirements on the dview control, so it is not supported.


DataTable binding in Winform:

For List <T>, you can easily modify the attributes of a set. For DataTable, this is a complicated custom class. What you want to bind is not the basic attribute of a table, it exists in another collection DataRowCollection.

Therefore, the designer has come up with a set of standards, such as PropertyDescriptor and ICustomTypeDescriptor, to realize the attribute description and the binding of custom class values.


WPF: not considering the support of DataTable:

It is estimated that the two sides did not communicate well, or considering that the new DataGrid was back to simplicity and removed the write requirement, WPF did not support DataTable. Fortunately, there was another defaview view attribute, you can read the information to the ableview of a similar set and bind it again. Although it takes another step, it barely supports binding.



Besides, MDataTable

The MDataTable is CYQ. A very important group of classes in the Data layer framework implements a complete ing with the database, and also handles various UIS. Of course, without it, I cannot study various bindings.

For a custom MDataTable data source, on the road of binding list controls, I studied it for a long time a few years ago. The most stable implementation was:

Public class MDataTable: IDataReader, IEnumerable, IListSource


Why does the MDataTable expire in the same way as the DataTable in WPF?

In fact, there is something different from DataTable, that is, my custom data source inherits IDataReader and IEnumerable, which simply supports WPF.


But why is the binding invalid?

After code debugging, we found that the original interface priority problem was a hot disaster. The level of IListSource was called before IEnumerable, and the result was a set of rules similar to the DataTable, however, the attribute reading of that set of rules is invalid for WPF, and the result is property binding instead of Data row binding.


Solution:

Because of the existence of IListSource, WPF cannot be properly bound, this interface is removed and WPF is normal.

However, the problem arises again. Winform can be bound only through the IListSource interface. If IListSource is removed, it is abnormal in winform.

After some thought, I considered that DataTable was bound to wpf by means of DataTableView. I showed a trick:


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2011501337-4.jpg "style =" line-height: 1.5; "/>

Note:

The implementation of the IListSource interface is divided into another class MDataTableView. Note that the modification is an internal class, indicating that for the user, everything is still the same, but internally in the MDataTable. the Bind method call identifies the current platform. If it is Winform, the object is changed to new MDataTableView (ref sourceObje)


Summary:

Although you usually work with these list controls, it is estimated that the binding mechanism is still relatively small. After all, it requires a certain fate.

Today's content will serve as a reference. When the fate of one day comes, you can review this article if necessary.


Related Article

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.