我们可以通过给AspxGridView的DataSource属性或DataSourceID赋值来实现数据绑定。
所支持的数据源类型包括:SqlDataSource、ObjectDataSource、XmlDataSource、AccessDataSource、SiteMapDatasource、LinqDataSource。下面我们为每一种类型举一个示例:
1.使用DataSource属性绑定DataTablestatic string _connectionstring = "数据库连接字符"
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据
currentConn.Open();
SqlDataAdapter currentSda = new SqlDataAdapter("select row_number() over(order by LogID) as RowNumber,* from Sys_Log", currentConn);
currentSda.Fill(currentDs, "Sys_Log");
this.ASPxGridView1.DataSource = currentDs.Tables["Sys_Log"];
this.ASPxGridView1.DataBind();
currentConn.Close();
}
2.使用DataSourceID属性绑定SqlDataSource<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="SqlDataSource1" >
<Columns >
<dxwgv:GridViewDataColumn Caption="LogID" FieldName="LogID" / >
<dxwgv:GridViewDataColumn Caption="发生时间" FieldName="OCCDatetime" / >
<dxwgv:GridViewDataColumn Caption="事件" FieldName="Event" / >
<dxwgv:GridViewDataColumn Caption="类型" FieldName="Type" / >
<dxwgv:GridViewDataColumn Caption="创建人" FieldName="Creator" / >
<dxwgv:GridViewDataColumn Caption="创建日期" FieldName="CreateDate" / >
<dxwgv:GridViewDataColumn Caption="备注" FieldName="Remark" / >
</Columns >
</dxwgv:ASPxGridView >
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="数据库连接字符"
SelectCommand="select row_number() over(order by LogID) as RowNumber,* from Sys_Log" >
</asp:SqlDataSource >
3.使用DataSourceID属性绑定ObjectDataSource<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="ObjectDataSource1" >
<Columns >
<dxwgv:GridViewDataColumn Caption="LogID" FieldName="LogID" / >
<dxwgv:GridViewDataColumn Caption="发生时间" FieldName="OCCDatetime" / >
<dxwgv:GridViewDataColumn Caption="事件" FieldName="Event" / >
<dxwgv:GridViewDataColumn Caption="类型" FieldName="Type" / >
<dxwgv:GridViewDataColumn Caption="创建人" FieldName="Creator" / >
<dxwgv:GridViewDataColumn Caption="创建日期" FieldName="CreateDate" / >
<dxwgv:GridViewDataColumn Caption="备注" FieldName="Remark" / >
</Columns >
</dxwgv:ASPxGridView >
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllLog"
typename="AspxGridView.SysLogDAL" / >
其中SysLogDAL.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace AspxGridView
{
public class SysLogDAL
{
static string _connectionstring = "数据库连接字符";
public static ICollection GetAllLog()
{
ArrayList al = new ArrayList();
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据
currentConn.Open();
SqlDataAdapter currentSda = new SqlDataAdapter("select * from Sys_Log", currentConn);
currentSda.Fill(currentDs, "Sys_Log");
DataTable dt = currentDs.Tables["Sys_Log"];
foreach (DataRow dr in dt.Rows)
{
SysLogEntry e = new SysLogEntry();
e.LogID = Int32.Parse(dr["LogID"].ToString());
e.OCCDatetime = DateTime.Parse(dr["OCCDatetime"].ToString());
e.Event = dr["Event"].ToString();
e.Type = dr["Type"].ToString();
e.Creator = dr["Creator"].ToString();
e.CreateDate = DateTime.Parse(dr["CreateDate"].ToString());
e.Remark = dr["Remark"].ToString();
al.Add(e);
}
currentConn.Close();
}
return al;
}
}
}
其中SysLogEntry.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AspxGridView
{
public class SysLogEntry
{
public int LogID
{
get;
set;
}
public DateTime OCCDatetime
{
get;
set;
}
public string Event
{
get;
set;
}
public string Type
{
get;
set;
}
public string Creator
{
get;
set;
}
public DateTime CreateDate
{
get;
set;
}
public string Remark
{
get;
set;
}
}
}
4.使用DataSourceID属性绑定LinqDataSource<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="LinqDataSource1" >
<Columns >
<dxwgv:GridViewDataColumn Caption="LogID" FieldName="LogID" / >
<dxwgv:GridViewDataColumn Caption="发生时间" FieldName="OCCDatetime" / >
<dxwgv:GridViewDataColumn Caption="事件" FieldName="Event" / >
<dxwgv:GridViewDataColumn Caption="类型" FieldName="Type" / >
<dxwgv:GridViewDataColumn Caption="创建人" FieldName="Creator" / >
<dxwgv:GridViewDataColumn Caption="创建日期" FieldName="CreateDate" / >
<dxwgv:GridViewDataColumn Caption="备注" FieldName="Remark" / >
</Columns >
</dxwgv:ASPxGridView >
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="AspxGridView.SysLogDAL" TableName="SysLogEntrys" >
</asp:LinqDataSource >
其中AspxGridView.SysLogDAL有一个SysLogEntrys的属性,代码如下(其它代码同例3)
public ICollection SysLogEntrys
{
get
{
return this.GetAllLog(); ;
}
}
5.使用DataSourceID属性绑定XmlDataSource<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="XmlDataSource1" >
<Columns >
<dxwgv:GridViewDataColumn Caption="LogID" FieldName="LogID" / >
<dxwgv:GridViewDataColumn Caption="发生时间" FieldName="OCCDatetime" / >
<dxwgv:GridViewDataColumn Caption="事件" FieldName="Event" / >
<dxwgv:GridViewDataColumn Caption="类型" FieldName="Type" / >
<dxwgv:GridViewDataColumn Caption="创建人" FieldName="Creator" / >
<dxwgv:GridViewDataColumn Caption="创建日期" FieldName="CreateDate" / >
<dxwgv:GridViewDataColumn Caption="备注" FieldName="Remark" / >
</Columns >
</dxwgv:ASPxGridView >
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Sys_Log.xml" ></asp:XmlDataSource >
其中Sys_Log.xml文件内容如下
<?xml version='1.0' encoding='gb2312'? >
<data >
<row LogID="1" OCCDatetime="2010-06-09 10:10:11.083" Event="用户【Wyf】登陆系统" Type="登陆信息" Creator="sa" CreateDate="2010-10-21 11:23:08.090" Remark="" / >
</data >