为AspxGridView绑定数据

前端之家收集整理的这篇文章主要介绍了为AspxGridView绑定数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为AspxGridView绑定数据

创建时间:2011年11月06日 21:13阅读次数:(3092)
分享到: 1
我们可以通过给AspxGridView的DataSource属性或DataSourceID赋值来实现数据绑定。

支持的数据源类型包括sqlDataSource、ObjectDataSource、XmlDataSource、AccessDataSource、SiteMapDatasource、LinqDataSource。下面我们为每一种类型举一个示例:

1.使用DataSource属性绑定DataTable
staticstring_connectionstring="数据库连接字符"
DataSetcurrentDs=newDataSet();
using(sqlConnectioncurrentConn=newsqlConnection(_connectionstring))
{
//获取数据
currentConn.Open();
sqlDataAdaptercurrentSda=newsqlDataAdapter("selectrow_number()over(orderbyLogID)asRowNumber,*fromSys_Log",currentConn);
currentSda.Fill(currentDs,"Sys_Log");

this.ASPxGridView1.DataSource=currentDs.Tables["Sys_Log"];
this.ASPxGridView1.DataBind();

currentConn.Close();
}


2.使用DataSourceID属性绑定sqlDataSource
<dxwgv:ASPxGridViewID="ASPxGridView1"runat="server"DataSourceID="sqlDataSource1">
<Columns>
<dxwgv:GridViewDataColumnCaption="LogID"FieldName="LogID"/>
<dxwgv:GridViewDataColumnCaption="发生时间"FieldName="OCCDatetime"/>
<dxwgv:GridViewDataColumnCaption="事件"FieldName="Event"/>
<dxwgv:GridViewDataColumnCaption="类型"FieldName="Type"/>
<dxwgv:GridViewDataColumnCaption="创建人"FieldName="Creator"/>
<dxwgv:GridViewDataColumnCaption="创建日期"FieldName="CreateDate"/>
<dxwgv:GridViewDataColumnCaption="备注"FieldName="Remark"/>
</Columns>
</dxwgv:ASPxGridView>
<asp:sqlDataSource
id="sqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="数据库连接字符"
SelectCommand="selectrow_number()over(orderbyLogID)asRowNumber,*fromSys_Log">
</asp:sqlDataSource>


3.使用DataSourceID属性绑定ObjectDataSource
<dxwgv:ASPxGridViewID="ASPxGridView1"runat="server"DataSourceID="ObjectDataSource1">
<Columns>
<dxwgv:GridViewDataColumnCaption="LogID"FieldName="LogID"/>
<dxwgv:GridViewDataColumnCaption="发生时间"FieldName="OCCDatetime"/>
<dxwgv:GridViewDataColumnCaption="事件"FieldName="Event"/>
<dxwgv:GridViewDataColumnCaption="类型"FieldName="Type"/>
<dxwgv:GridViewDataColumnCaption="创建人"FieldName="Creator"/>
<dxwgv:GridViewDataColumnCaption="创建日期"FieldName="CreateDate"/>
<dxwgv:GridViewDataColumnCaption="备注"FieldName="Remark"/>
</Columns>
</dxwgv:ASPxGridView>
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllLog"
typename="AspxGridView.SysLogDAL"/>


其中SysLogDAL.cs代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Data;
usingSystem.Data.sqlClient;
usingSystem.Collections;

namespaceAspxGridView
{
publicclassSysLogDAL
{
staticstring_connectionstring="数据库连接字符";
publicstaticICollectionGetAllLog()
{
ArrayListal=newArrayList();
DataSetcurrentDs=newDataSet();
using(sqlConnectioncurrentConn=newsqlConnection(_connectionstring))
{
//获取数据
currentConn.Open();
sqlDataAdaptercurrentSda=newsqlDataAdapter("select*fromSys_Log","Sys_Log");

DataTabledt=currentDs.Tables["Sys_Log"];
foreach(DataRowdrindt.Rows)
{
SysLogEntrye=newSysLogEntry();
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();
}
returnal;
}
}
}


其中SysLogEntry.cs代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;

namespaceAspxGridView
{
publicclassSysLogEntry
{
publicintLogID
{
get;
set;
}
publicDateTimeOCCDatetime
{
get;
set;
}
publicstringEvent
{
get;
set;
}
publicstringType
{
get;
set;
}
publicstringCreator
{
get;
set;
}
publicDateTimeCreateDate
{
get;
set;
}
publicstringRemark
{
get;
set;
}
}
}


4.使用DataSourceID属性绑定LinqDataSource
<dxwgv:ASPxGridViewID="ASPxGridView1"runat="server"DataSourceID="LinqDataSource1">
<Columns>
<dxwgv:GridViewDataColumnCaption="LogID"FieldName="LogID"/>
<dxwgv:GridViewDataColumnCaption="发生时间"FieldName="OCCDatetime"/>
<dxwgv:GridViewDataColumnCaption="事件"FieldName="Event"/>
<dxwgv:GridViewDataColumnCaption="类型"FieldName="Type"/>
<dxwgv:GridViewDataColumnCaption="创建人"FieldName="Creator"/>
<dxwgv:GridViewDataColumnCaption="创建日期"FieldName="CreateDate"/>
<dxwgv:GridViewDataColumnCaption="备注"FieldName="Remark"/>
</Columns>
</dxwgv:ASPxGridView>
<asp:LinqDataSourceID="LinqDataSource1"runat="server"
ContextTypeName="AspxGridView.SysLogDAL"TableName="SysLogEntrys">
</asp:LinqDataSource>

其中AspxGridView.SysLogDAL有一个SysLogEntrys的属性代码如下(其它代码同例3)
publicICollectionSysLogEntrys
{
get
{
returnthis.GetAllLog();;
}
}


5.使用DataSourceID属性绑定XmlDataSource
<dxwgv:ASPxGridViewID="ASPxGridView1"runat="server"DataSourceID="XmlDataSource1">
<Columns>
<dxwgv:GridViewDataColumnCaption="LogID"FieldName="LogID"/>
<dxwgv:GridViewDataColumnCaption="发生时间"FieldName="OCCDatetime"/>
<dxwgv:GridViewDataColumnCaption="事件"FieldName="Event"/>
<dxwgv:GridViewDataColumnCaption="类型"FieldName="Type"/>
<dxwgv:GridViewDataColumnCaption="创建人"FieldName="Creator"/>
<dxwgv:GridViewDataColumnCaption="创建日期"FieldName="CreateDate"/>
<dxwgv:GridViewDataColumnCaption="备注"FieldName="Remark"/>
</Columns>
</dxwgv:ASPxGridView>
<asp:XmlDataSourceID="XmlDataSource1"runat="server"DataFile="~/Sys_Log.xml"></asp:XmlDataSource>


其中Sys_Log.xml文件内容如下
<?xmlversion='1.0'encoding='gb2312'?> <data> <rowLogID="1"OCCDatetime="2010-06-0910:10:11.083"Event="用户【Wyf】登陆系统"Type="登陆信息"Creator="sa"CreateDate="2010-10-2111:23:08.090"Remark=""/> </data>
原文链接:https://www.f2er.com/xml/298653.html

猜你在找的XML相关文章