DataGrid绑定有很多种,可以用DataView,DataReader,DataSet这些来绑定,还可以使用自定义控件来绑定,其实说穿了也就是数据对应显示。而分页也有很多种:1、使用sql语句,select top 10 from xxx where xxx,后面就加not in (select top 10 from xxx where xxx)。2、使用一次读取数据,然后通过程序控制分页。
下面是一个简单分页以及绑定的过程:
先创建DataGrid
1
<
asp:DataGrid AllowPaging
=
"
true
"
AllowSorting
=
"
true
"
AutoGenerateColumns
=
"
false
"
Width
=
"
100%
"
OnPageIndexChanged
=
"
changepage
"
2 FooterStyle - BackColor = " #0099FF " ID = " datagrid1 " ItemStyle - BackColor = " #006699 " PagerStyle - NextPageText = " 下一页 " PagerStyle - PrevPageText = " 上一页 " PageSize = " 20 " runat = " server " >
3 < columns >
4 < asp:BoundColumn HeaderText = " 用户编号 " HeaderStyle - Width = " 100 " ItemStyle - Width = " 50 " DataField = " _userid " />
5 < asp:BoundColumn HeaderText = " 用户名 " HeaderStyle - Width = " 100 " ItemStyle - Width = " 100 " DataField = " id " />
6 < asp:BoundColumn HeaderText = " 电子邮件 " HeaderStyle - Width = " 150 " ItemStyle - Width = " 150 " DataField = " email " />
7 < asp:BoundColumn HeaderText = " 类型 " HeaderStyle - Width = " 50 " ItemStyle - Width = " 50 " DataField = " type " />
8 </ columns >
9 </ asp:DataGrid >
2 FooterStyle - BackColor = " #0099FF " ID = " datagrid1 " ItemStyle - BackColor = " #006699 " PagerStyle - NextPageText = " 下一页 " PagerStyle - PrevPageText = " 上一页 " PageSize = " 20 " runat = " server " >
3 < columns >
4 < asp:BoundColumn HeaderText = " 用户编号 " HeaderStyle - Width = " 100 " ItemStyle - Width = " 50 " DataField = " _userid " />
5 < asp:BoundColumn HeaderText = " 用户名 " HeaderStyle - Width = " 100 " ItemStyle - Width = " 100 " DataField = " id " />
6 < asp:BoundColumn HeaderText = " 电子邮件 " HeaderStyle - Width = " 150 " ItemStyle - Width = " 150 " DataField = " email " />
7 < asp:BoundColumn HeaderText = " 类型 " HeaderStyle - Width = " 50 " ItemStyle - Width = " 50 " DataField = " type " />
8 </ columns >
9 </ asp:DataGrid >
其中AllowPaging是允许自动分页,AllowSorting是允许自动排序,AutoGenerateColumns是允许自动生成列(如何要自己定义列这里就要设为false),后面就是分页的事件,PagerStyle-mode是分页时候下面显示的类型,默认是上一页,下一页那种,还有就是NumericPages,显示的是12345那种类型。下面的columns中就是要显示的列,后面DataField就是要绑定的字段名。
然后就是写Page_Load代码,一开始就要绑定,所以就是查询数据库获取数据
1
string
sql
=
"
select _userid,id,type,email from user where city!='空' order by _userid desc
"
;
2 string connstr = " uid=sa;password=123;database=kkk;server=(local) " ;
3 SqlConnection conn = new SqlConnection(connstr);
4 SqlDataAdapter comm = new SqlDataAdapter(sql,conn);
5 DataSet isset = new DataSet();
6 comm.Fill(isset, " User " );
7 DataView isview = new DataView(isset.Tables[ " User " ]);
8 datagrid1.DataSource = isview;
9 datagrid1.DataBind();
2 string connstr = " uid=sa;password=123;database=kkk;server=(local) " ;
3 SqlConnection conn = new SqlConnection(connstr);
4 SqlDataAdapter comm = new SqlDataAdapter(sql,conn);
5 DataSet isset = new DataSet();
6 comm.Fill(isset, " User " );
7 DataView isview = new DataView(isset.Tables[ " User " ]);
8 datagrid1.DataSource = isview;
9 datagrid1.DataBind();
这里就是使用DataView的方式绑定的,先定义连接,再通过SqlDataAdapter读取数据,再把数据填充到DataSet中的User表中,把DataSet表中的内容给DataView,然后就绑定DataGrid。其实这很简单,在我前面发的DataGrid和XML读取数据中有读取XML数据绑定的方法,其中还可以设置Sort来排顺。
接下来是定义DataGrid中分页的事件
1
datagrid1.CurrentPageIndex
=
e.NewPageIndex;
2 binding();
其中的binding()就是上面的绑定代码,这里可以把之前的绑定代码写成函数来调用。 2 binding();
这样就完成了简单的数据读取,绑定以及分页。
在使用的过程中我们有时候需要把查询的数据进行处理后显示,比如查询到用户的类型1,但是我们需要看到是普通用户还是特殊用户,这里就需要使用自定义控件来时间了。
1
<%
@ Register TagPrefix
=
"
user
"
TagName
=
"
money
"
Src
=
"
xxx.ascx
"
%>
2
3 < asp:TemplateColumn HeaderText = " 充值数量 " HeaderStyle - Width = " 70 " ItemStyle - Width = " 200 " >
4 < itemtemplate >< user:money id = " check1 " runat = " server " uid =<% # DataBinder.Eval(Container.DataItem, " id " ) %>/>
5 </ itemtemplate >
6 </ asp:TemplateColumn >
2
3 < asp:TemplateColumn HeaderText = " 充值数量 " HeaderStyle - Width = " 70 " ItemStyle - Width = " 200 " >
4 < itemtemplate >< user:money id = " check1 " runat = " server " uid =<% # DataBinder.Eval(Container.DataItem, " id " ) %>/>
5 </ itemtemplate >
6 </ asp:TemplateColumn >
这里就是先编写xxx.ascx,然后在网页中注册标签(第一行就是),然后在DataGrid中使用,这里要使用asp:TemplateColumn来绑定。其中的<%# DataBinder.Eval(Container.DataItem,"id")%>就是绑定的内容,而前面的就是引用自定义的控件。顺便说一下columns中的绑定方法:之前示范的是一种,还有的是使用最先的DataFiled的方法,还有一种就是之前示范的用asp:TemplateColumn,不过在itemtemplate中用<%# DataBinder.Eval(Container.DataItem,"id")%>
以上就是一些简单的DataGrid使用方法,大家交流交流。