Which objects can be used as data sources of data controls?

Source: Internet
Author: User

After several years of data binding, we will summarize which objects can be used as data sources for data control. The following text is from Microsoft's msdn.

Use the datasource attribute to specify the source of the value to be bound to the data list control. The data source must be system. collections. ienumerable interface (for example, system. data. dataview, system. collections. arraylist or system. collections. hashtable) or ilistsource interface object, can be bound to the control derived from the basedatalist class. When setting the datasource attribute, you must manually write code to bind data.

If the data source specified by the datasource attribute contains multiple data sources, use the datamember attribute to specify the specific source to be bound to the control. For example, if a system. Data. DataSet object contains multiple tables, you must specify the table to be bound to the control. After the data source is specified, use the databind method to bind the data source to the control.

From the above text, we can know which objects can be used as data sources, but it is relatively obscure for beginners. Now, based on your own experience, Let's explain:

We often use the following objects: dataset, able, and dataview. They all implement interfaces: ilistsource or ienumerable, and Microsoft has well encapsulated them for us, you don't need to worry too much about the usage, or even know what interfaces they implement (if you want to know it, check it in msdn ).

The problem is what if we use a generic list? It also implements the ienumerable interface, but you will think that any object in the list can be used as a data source? Let's look at the following code:

Class student
{
Public student (string N, string S)
{
Name = N;
Sex = s;
}
Public string name;

Public String sex;

}

Can the following binding be successful?

List <student> List = new list <student> ();
List. Add (new student ("Hebei", "1 "));
List. Add (new student ("Beijing", "2 "));

Gridview1.datasource = List;

Gridview1.databind ();

The following error message is displayed during execution:

The data source of the gridview with the ID "gridview1" does not have any attributes or features that can be used to generate columns. Make sure that your data source has content.

What's going on? Cannot list be used as the data source of the gridview? Wrong!

Modify the class definition Code as follows:

Class student
{
Public student (string N, string S)
{
Name = N;
Sex = s;
}
Private string name;

Public string name
{
Get {return name ;}
Set {name = value ;}
}
Private string sex;

Public String sex
{
Get {return sex ;}
Set {sex = value ;}
}
}

There is no problem with the execution.

When using the list for storing custom classes, you must note that the custom classes must contain the information to be bound-Public attributes.

 

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.