欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » SmartGrid使用教程 » 正文

SmartGrid教程(三):设置与绑定数据源

创建时间:2012年03月08日 17:21  阅读次数:(5378)
分享到:

为SmartGrid控件设置与绑定数据源(DataSource)


SmartGrid的DataSource属性用来存取或设置数据源,数据源的类型可以为XML文档、DataTable、DataSet、SqlDataReader、OdbcDataReader、OleDataReader等。实现了System.ComponentModel.IListSource或System.Collections.IEnumerable接口或继承自System.Xml.XmlElement的对象都可作为其数据源。

注意:如果DataSource类型为DataSet,且同时指定了DataMember属性(数据表名),则使用DataSet中指定的数据表填充数据,否则取第一个数据表(Tables[0])填充数据

下面我们就为SmartGrid绑定DataTable,XML,DataReader,DataSet各举一个实例,其它类型数据源,大家自己举一反三了。

(1)为SmartGrid绑定DataTable示例:
string _connectionstring="Data Source=192.168.1.1;Initial Catalog=test;UID=sa;Password=xxxxxx";
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据 
currentConn.Open();
SqlDataAdapter currentSda = new SqlDataAdapter("select * from table1", currentConn);
currentSda.Fill(currentDs, "table1");

this.SmartGrid1.DataSource = currentDs.Tables["table1"];
this.SmartGrid1.DataBind();
}


(2)为SmartGrid绑定XML示例:
protected void btnLoadData_XML_Click(object sender, EventArgs e)
{
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据 
currentConn.Open();
SqlDataAdapter currentSda = new SqlDataAdapter("select * from table1", currentConn);
currentSda.Fill(currentDs, "table1");

XmlDocument xmlDocument = MyConvertDataTableToXML(currentDs.Tables["table1"]);
this.SmartGrid1.DataSource = xmlDocument.DocumentElement;
this.SmartGrid1.DataBind();
}
}


(3)为SmartGrid绑定DataReader示例:
protected void btnLoadData_SqlDataReader_Click(object sender, EventArgs e)
{
string selectCmd = "select * from table1";
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据 
currentConn.Open();
SqlCommand currentCmd = new SqlCommand();
currentCmd.CommandType = CommandType.Text;
currentCmd.CommandText = selectCmd;
currentCmd.Connection = currentConn;

SqlDataReader currentSdr = currentCmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(currentSdr);
currentSdr.Close();

this.SmartGrid1.DataSource = dt;
this.SmartGrid1.DataBind();
}
}


(4)为SmartGrid绑定DataSet示例:
protected void btnLoadData_DataSet_Click(object sender, EventArgs e)
{
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据 
currentConn.Open();
SqlDataAdapter currentSda = new SqlDataAdapter("select * from table1", currentConn);
currentSda.Fill(currentDs, "table1");

this.SmartGrid1.DataSource = currentDs;
this.SmartGrid1.DataBind();
}
}


以上是为SmartGrid绑定四种不同数据源的示例,供大家参考。w
来源:.net学习网
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf】

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论10条
  • #1楼  评论人:匿名  评论时间:2012-5-2 10:46:54
  • 在取数据源的时候,报“未将对象引用设置到对象的实例”,如果取消掉OnRowDeleted之类的判断,就好了。请问lz有没有碰到过。
  • #2楼  评论人:Wyf  评论时间:2012-5-2 19:10:47
  • 是在cs文件中报出的异常吗?打个断点调试一下就知道了。
  • #3楼  评论人:匿名  评论时间:2012-5-3 13:57:30
  • DataTable dt = SmartGrid1.DataSource as DataTable; 这里就报错了,断不了。
  • #4楼  评论人:匿名  评论时间:2012-5-3 14:13:26
  • function RowDeleted(grid,ev) { } 这个是删除判断的,但是这里面什么代码都没有,一样报错,除非在grid里面取消事件判断。
  • #5楼  评论人:Wyf  评论时间:2012-5-3 18:43:26
  • 将DataTable dt = SmartGrid1.DataSource as DataTable if (SmartGrid1.DataSource!=null) { DataTable dt = SmartGrid1.DataSource as DataTable } 试试
  • #6楼  评论人:Wyf  评论时间:2012-5-3 18:44:00
  • 将DataTable dt = SmartGrid1.DataSource as DataTable改成 if (SmartGrid1.DataSource!=null) { DataTable dt = SmartGrid1.DataSource as DataTable } 试试
  • #7楼  评论人:匿名  评论时间:2012-5-4 9:07:09
  • 这样是没错,但是并不是我要的结果,这样数据都丢失了。
  • #8楼  评论人:Wyf  评论时间:2012-5-5 23:17:32
  • 单从代码上来说,加个if完全不会造成你的数据丢失,因为这只是一个判断语句而已。你应该要检查为什么SmartGrid.DataSource会等于null.
  • #9楼  评论人:匿名  评论时间:2012-5-7 8:59:41
  • 是的,就是不知道为什么会这样,只是空函数都会出问题。
  • #10楼  评论人:Wyf  评论时间:2012-5-7 17:25:45
  • 我做了测试,并没有出现你所说的情况呢。 设置OnRowDeleted后,在cs代码中还是可以取到SmartGrid的值的。
发表评论:
留言人:
内  容:
请输入问题 25+83=? 的结果(结果是:108)
结  果: