如何从List对象绑定到DataTable? [英] How to bind a ListBox to a DataTable from a session object?
本文介绍了如何从List对象绑定到DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个会话对象,包含我上一页的一个DataTable,
,我想将此DataTable绑定到一个ListBox。
v完成此操作:
protected void Page_Load(object sender,EventArgs e)
{
if(! IsPostBack)
{
if(Session [bestStocks]!= null)
{
DataTable dt = new DataTable();
dt =(DataTable)会话[bestStocks];
DataView dv = new DataView(dt);
BestStockslb.DataSource = dt;
BestStockslb.DataBind();
}
}
}
我得到这个结果: / p>
任何建议?
谢谢,
liron
解决方案
似乎你已经忘记了DataTextField和DataValueField
dt =(DataTable)Session [ bestStocks];
DataView dv = new DataView(dt);
BestStockslb.DataSource = dt;
BestStockslb.DataTextField =Name;
BestStockslb.DataValueField =ID;
BestStockslb.DataBind();
I have a session object that contains a DataTable from my previous page, and i would like to bind this DataTable to a ListBox.
I'v done this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["bestStocks"] !=null)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["bestStocks"];
DataView dv = new DataView(dt);
BestStockslb.DataSource = dt;
BestStockslb.DataBind();
}
}
}
I get this result:
Any suggestion?
thanks, liron
解决方案
It seems you have forgot the DataTextField and DataValueField
dt = (DataTable)Session["bestStocks"];
DataView dv = new DataView(dt);
BestStockslb.DataSource = dt;
BestStockslb.DataTextField = "Name";
BestStockslb.DataValueField = "ID";
BestStockslb.DataBind();
这篇关于如何从List对象绑定到DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文