http://pin5i.com/showtopic-21778.html
数据绑定控件,都有DataSource属性,用于指定数据源。通常情况下,也是比较常用的就是数据源是数据表(DataTable),其实DataSource的范围很广,我们可以在深入研究的同时,改善我们的程序。
有的时候,你在编程进入一定阶段,进一步提升很困难的境况之下,不妨回过头来看看基础的东西,或许你会有新的受益,或许能够真正的体会到孔夫子所谓的“温故而知新”的真正内涵。
常用的数据绑定控件有:Repeater、DataList、GridView、DetailsView等,在这里我拿Repeater来简单说明问题。
使用该属性指定用来填充 Repeater 控件的数据源。DataSource 可以是任何 System.Collections.IEnumerable 对象,
如用于访问数据库的 System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、数组或 IListSource 对象。
常用的数据源:
一个 DataTable
一个 DataView
一个 DataSet
任何实现 IListSource 接口的组件
任何实现 IList 接口的组件
注意:
若要绑定到对象的强类型数组,该对象类型必须包含公共属性。
下面通过几个简单的实例来介绍DataSource的具体应用。
<1>绑定DataTable,一般都是从数据库取出数据,然后直接进行绑定,具体的数据库操作的逻辑不再提供。想必大家都已经非常熟悉。绑定DataView与这个类似。 private void BindData()
{
//通过业务逻辑,直接调用数据库中的数据
DataTable nTable = getTable();
Repeater1.DataSource = nTable;
Repeater1.DataBind();
}
复制代码Html代码
Repeater <asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<th scope="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、一维数组之类,里面存储简单的数据。
ArrayList private void BindData()
{
ArrayList list = new ArrayList();
list.Add("Jim");
list.Add("Tom");
list.Add("Bluce");
list.Add("Mary");
Repeater1.DataSource = list;
Repeater1.DataBind();
}
复制代码Html适当改变 <asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><table><tr><th scope="col">姓名</th></tr></HeaderTemplate>
<ItemTemplate><tr><td><%#Container.DataItem %></td></tr></ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
复制代码<3>绑定Dictionary、HashTable
Dictionary private void BindData()
{
Dictionary<string, int> dic = new Dictionary<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:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><table><tr><th scope="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等。这个很是有用,在我们进行数据查询的时候,经常从数据库取出数据,为了方便操作,需要封装成对象,但是有的时候需要将这些对象以列表的形式显示出来,一种解决方案:对象转换为DataTable,另一种就是直接调用数据库。这两种方案,并不是很理想。而这里直接将对象集合直接绑定到数据显示控件,给我指明一条出路。其实,在PetShop4.0就是利用这一点,绑定ICollection或者IList。简单明了。
一个简单的用户类,包含两个公共属性。
User using System;
using System.Data;
/// <summary>
/// Summary description for User
/// </summary>
public class User
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private int _Age;
public int Age
{
get { return _Age; }
set { _Age = value; }
}
public User()
{
//
// TODO: Add constructor logic here
//
}
public User(string name,int age)
{
_Name = name;
_Age = age;
}
}
复制代码绑定对象集合:
IList private void BindData()
{
User user1 = new User("Jim", 21);
User user2 = new User("Tom",23);
User user3 = new User("Bluce",33);
User user4 = new User("Mary",18);
IList<User> list = new List<User>();
list.Add(user1);
list.Add(user2);
list.Add(user3);
list.Add(user4);
Repeater1.DataSource = list;
Repeater1.DataBind();
}
复制代码对应的Repeater绑定对象的公共属性: <asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<th scope="col">
姓名</th>
<th>
年龄</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("Name") %>
</td>
<td>
<%#Eval("Age") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
复制代码
分享到:
相关推荐
要求通过DataSource控件将数据库中的数据绑定到GridView中,并实现数据的更新和删除
常用的C#数据绑定控件有:Repeater、DataList、GridView、DetailsView等,在这里我拿Repeater来简单说明问题。使用该属性指定用来填充Repeater控件的数据源。DataSource可以是任何System.Collections.IEnumerable对象...
V1.2 请到http://download.csdn.net/source/1022630下载。具体实现请参考拙文:http://blog.csdn.net/hulihui/archive/2009/02/01/3856824.aspx。
在使用ComboBox控件时,遇到了重新绑定赋值出问题的情况。 错误代码如下: if (CustomerBLL.select().Rows.Count > 0) { cbTcid.Items.Clear(); cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = ...
下面将比较重要的属性小结如下: <BR><BR> DataSource---------用来指定需要绑定的数据源,比如data控件。 Cellpicture----------用来设定当前单元格的图象,便于显示该图象。此属性不能在设计时使用。 Col,...
TreeView控件缺点是无法快速应用于数据库绑定显示数据及即时编码,这个增强型控件简化了复杂的数据关联显示,我们可以灵活的绑定数据库,减少编码量.主要特点有: A.支持简单的和复杂的数据关联 B.递归纪录连接 C.一...
DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset 对象的记录和字段。可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在...
// 设置数据源 // 绑定数据源 dataGridView1为空间的name dataGridView1.DataSource = daset.Tables["表名"];
8.1.4 使用其他数据绑定控件 235 8.2 使用DataSource控件 237 8.3 使用编程式数据绑定 240 8.4 模板和数据绑定表达式 241 8.4.1 使用模板 241 8.4.2 使用数据绑定表达式 244 8.4.3 使用两种数据绑定表达式 247 8.5 ...
使用DataSource属性,可从数据库或数组中读取图表。内部的过滤让可提供的数据模式于所需绘制的图表选择类型相匹配。更可让开发者在图表绘制中调整行和列的包含/非包含的情况。 高级安全性 UltraWebChart提供了一...
如为GridView、DataList以及Repeater等数据绑定控件实现分页、呈现自定义的分页数据以及制作图片浏览程序等,因为AspNetPager控件和数据是独立的,因此要分页的数据可以来自任何数据源,如SQL Server、Oracle、...
本文实例讲述了WinForm中comboBox控件数据绑定实现方法。分享给大家供大家参考,具体如下: 下面介绍三种对comboBox绑定的方式,分别是泛型中IList和Dictionary,还有数据集DataTable 一、IList 现在我们直接创建...
二、AspNetPager支持各种数据绑定控件GridView、DataGrid、DataList、Repeater以及自定义的数据绑定控件的分页功能十分强大。 三、AspNetPager分页控件本身并不显示任何数据,而只显示分页导航元素,数据在页面上的...
2、datasource 绑定 datasource 3、dbgrid 绑定 dbgrid 4、stringgrid 绑定 stringgrid PrintObject 选择打印的对象(datasource 、dbgrid、 stringgrid ) 5、pagefooter 页脚 pageheader 页眉 (1) Text:...
将DataTable绑定到丰富的数据控件
给ASP.NET中的TreeView绑定DataSet或ObjectDataSource对象
-修正绑定到Tree的XMLDocument中Icon属性映射错误(feedback:nopnop9)。 -修正HtmlEditor不能编辑的BUG(feedback:TheBox)。 -修正IE下有时会出现空白页面的情况(feedback:olivia919)。 +2009-12-06 v2.1.8 -...
//绑定数据-2_只修改Sql语句即可 public void BindData() { SqlConnection conn = new SqlConnection(connstring); string sql = "select * from tuiguang_2";//Sql查询语句(必修改) SqlDataAdapter da = ...
将对象列表绑定到XtraGrid控件的DataSource属性上的实例!