您好,登錄后才能下訂單哦!
C#中怎么利用DataSource屬性綁定數(shù)據(jù),很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
常用的C#數(shù)據(jù)綁定控件有:Repeater、DataList、GridView、DetailsView等,在這里我拿Repeater來簡單說明問題。
使用該屬性指定用來填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對象,
如用于訪問數(shù)據(jù)庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對象。
常用的數(shù)據(jù)源:
一個DataTable
一個DataView
一個DataSet
任何實現(xiàn)IListSource接口的組件
任何實現(xiàn)IList接口的組件
注意:
若要綁定到對象的強類型數(shù)組,該對象類型必須包含公共屬性。
下面通過幾個簡單的實例來介紹DataSource的具體應(yīng)用。
<1>綁定DataTable,一般都是從數(shù)據(jù)庫取出數(shù)據(jù),然后直接進行綁定,具體的數(shù)據(jù)庫操作的邏輯不再提供。想必大家都已經(jīng)非常熟悉。綁定DataView與這個類似。
程序代碼
privatevoidBindData() { //通過業(yè)務(wù)邏輯,直接調(diào)用數(shù)據(jù)庫中的數(shù)據(jù) DataTablenTable=getTable(); Repeater1.DataSource=nTable; Repeater1.DataBind(); }
HTML代碼
C#數(shù)據(jù)綁定控件程序代碼
<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> <HeaderTemplate> <table> <tr> <thscopethscope="col"> 姓名th> <th> 年齡th> tr> HeaderTemplate> <ItemTemplate> <tr> <td> <%#Eval("Key")%> td> <td> <%#Eval("value")%> td> tr> ItemTemplate> <FooterTemplate> table>FooterTemplate> asp:Repeater>
<2>綁定Array、ArrayList、List、一維數(shù)組之類,里面存儲簡單的數(shù)據(jù)。
ArrayList
C#數(shù)據(jù)綁定控件程序代碼
privatevoidBindData() { ArrayListlist=newArrayList(); list.Add("Jim"); list.Add("Tom"); list.Add("Bluce"); list.Add("Mary"); Repeater1.DataSource=list; Repeater1.DataBind(); }
HTML適當(dāng)改變
程序代碼
<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> <HeaderTemplate><table><tr><thscopethscope="col">姓名th>tr>HeaderTemplate> <ItemTemplate><tr><td><%#Container.DataItem%>td>tr>ItemTemplate> <FooterTemplate>table>FooterTemplate> asp:Repeater>
<3>綁定Dictionary、HashTable
Dictionary
C#數(shù)據(jù)綁定控件程序代碼
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代碼
程序代碼
<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> <HeaderTemplate><table><tr><thscopethscope="col">姓名th><th>年齡th>tr>HeaderTemplate> <ItemTemplate><tr><td><%#Eval("Key")%>td><td><%#Eval("value")%>td>tr>ItemTemplate> <FooterTemplate>table>FooterTemplate> asp:Repeater>
<4>綁定對象集合,IList等。這個很是有用,在我們進行數(shù)據(jù)查詢的時候,經(jīng)常從數(shù)據(jù)庫取出數(shù)據(jù),為了方便操作,需要封裝成對象,但是有的時候需要將這些對象以列表的形式顯示出來,一種解決方案:對象轉(zhuǎn)換為DataTable,另一種就是直接調(diào)用數(shù)據(jù)庫。這兩種方案,并不是很理想。而這里直接將對象集合直接綁定到數(shù)據(jù)顯示控件,給我指明一條出路。其實,在PetShop4.0就是利用這一點,綁定ICollection或者IList。簡單明了。
一個簡單的用戶類,包含兩個公共屬性。
程序代碼
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; } }
綁定對象集合:
IList
程序代碼
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(); }
對應(yīng)的Repeater綁定對象的公共屬性:
C#數(shù)據(jù)綁定控件程序代碼
<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server"> <HeaderTemplate> <table> <tr> <thscopethscope="col"> 姓名th> <th> 年齡th> tr> HeaderTemplate> <ItemTemplate> <tr> <td> <%#Eval("Name")%> td> <td> <%#Eval("Age")%> td> tr> ItemTemplate> <FooterTemplate> table>FooterTemplate> asp:Repeater>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。