A brief introduction to the DataSource property in the C data bound control

  • 2020-05-26 08:15:40
  • OfStack

Sometimes, when you are in the programming stage 1, step 1 to improve the situation is very difficult, you might as well go back to look at the basic things, maybe you will have new benefits, maybe you can really understand the meaning of Confucius' so-called "review the old and learn the new".
The commonly used C# data bound controls are Repeater, DataList, GridView, DetailsView, etc.
Use this property to specify the data source to populate the Repeater control. DataSource can be any System.Collections.IEnumerable object,
Such as System.Data.DataView, System.Collections.ArrayList, System.Collections.Hashtable, array, or IListSource objects for database access.
Common data sources:
1 DataTable
1 DataView
1 DataSet
Any component that implements the IListSource interface
Any component that implements the IList interface
Note:
To bind to a strongly typed array of an object, the object type must contain public properties.
The specific application of DataSource is introduced by a few simple examples below.
< 1 > Binding DataTable,1 is generally from the database to extract data, and then directly bind, the specific database operation logic is no longer provided. I'm sure you're all familiar with it. Binding DataView is similar to this.
The program code


privatevoidBindData()  
{  
// The data in the database is called directly through the business logic   
DataTablenTable=getTable();  

Repeater1.DataSource=nTable;  
Repeater1.DataBind();  
} 

HTML code
C# data bound control program code


<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate> 
<table> 
<tr> 
<thscopethscope="col"> 
 The name th> 
<th> 
 age th> 
<tr> 
<HeaderTemplate> 
<ItemTemplate> 
<tr> 
<td> 
<%#Eval("Key")%> 
<td> 
<td> 
<%#Eval("value")%> 
<td> 
<tr> 
<ItemTemplate> 
<FooterTemplate> 
<table><FooterTemplate> 
<asp:Repeater> 

< 2 > Bind Array, ArrayList, List, 1-dimensional arrays, etc., to store simple data.
ArrayList
C# data bound control program code


privatevoidBindData()  
{  
ArrayListlist=newArrayList();  
list.Add("Jim");  
list.Add("Tom");  
list.Add("Bluce");  
list.Add("Mary");  

Repeater1.DataSource=list;  
Repeater1.DataBind();  
} 

Change HTML as appropriate
The program code

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate><table><tr><thscopethscope="col"> The name <th><tr><HeaderTemplate> 
<ItemTemplate><tr><td><%#Container.DataItem%><td><tr><ItemTemplate> 
<FooterTemplate><table><FooterTemplate> 
<asp:Repeater> 

< 3 > Bind Dictionary, HashTable
Dictionary
C# data bound control program code

privatevoidBindData()  
{  
Dictionary<string,int>dic=newDictionary<string,int>();  
dic.Add("Jim",21);  
dic.Add("Tom",26);  
dic.Add("Bluce",33);  
dic.Add("Mary",18);  

Repeater1.DataSource=dic;  
Repeater1.DataBind();  
} 

HTML code
The program code

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate><table><tr><thscopethscope="col"> The name <th><th> age <th><tr><HeaderTemplate> 
<ItemTemplate><tr><td><%#Eval("Key")%>td><td><%#Eval("value")%><td><tr><ItemTemplate> 
<FooterTemplate><table><FooterTemplate> 
<asp:Repeater>

< 4 > Binding object collection, IList, etc. This is very useful, when we conduct data query, often take data from the database, in order to facilitate the operation, need to encapsulate into objects, but sometimes need to display these objects in the form of a list, one solution: object conversion to DataTable, another is to directly call the database. These two solutions are not ideal. Here, the collection of objects is directly bound to the data display control, giving me a way out. In fact, in PetShop 4.0, this point is used to bind ICollection or IList. Simple and clear.
A simple user class with two common properties.
The program code

usingSystem;  
usingSystem.Data;  

///

///SummarydescriptionforUser  
///

publicclassUser  
{  
privatestring_Name;  
publicstringName  
{  
get{return_Name;}  
set{_Name=value;}  
}  
privateint_Age;  
publicintAge  
{  
get{return_Age;}  
set{_Age=value;}  
}  
publicUser()  
{  
//  
//TODO:Addconstructorlogichere  
//  
}  
publicUser(stringname,intage)  
{  
_Name=name;  
_Age=age;  
}  
} 

Binding object collection:
IList
The program code

privatevoidBindData()  
{  
Useruser1=newUser("Jim",21);  
Useruser2=newUser("Tom",23);  
Useruser3=newUser("Bluce",33);  
Useruser4=newUser("Mary",18);  

IList<User>list=newList<User>();  
list.Add(user1);  
list.Add(user2);  
list.Add(user3);  
list.Add(user4);  

Repeater1.DataSource=list;  
Repeater1.DataBind();  
} 

The public properties of the corresponding Repeater binding object:
C# data bound control program code

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> 
<HeaderTemplate> 
<table> 
<tr> 
<thscopethscope="col"> 
 The name th> 
<th> 
 age <th> 
<tr> 
<HeaderTemplate> 
<ItemTemplate> 
<tr> 
<td> 
<%#Eval("Name")%> 
<td> 
<td> 
<%#Eval("Age")%> 
<td> 
<tr> 
<ItemTemplate> 
<FooterTemplate> 
<table><FooterTemplate> 
<asp:Repeater> 


Related articles: