How to bind a ListBox to a DataTable from a session object?

家住魔仙堡 提交于 2019-12-05 21:46:40

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();

Change this line:

BestStockslb.DataSource = dt;

To:

BestStockslb.DataSource = dt.DefaultView;

And you also need to set the DataTextField and DataValueField Properties of BestStockslb to link to the required fields in the returned data. This is why you are getting the DataRowView output.

You could also remove DataView dv = new DataView(dt); as it looks like you are not using it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!