开发者

Change only the DataValueField of a Listbox?

开发者 https://www.devze.com 2023-02-13 17:46 出处:网络
I have a Listbox dimension.DataSource = provider.DimensionList; dimension.DataBind(); I want the value to be the ID of these elements

I have a Listbox

dimension.DataSource = provider.DimensionList;
dimension.DataBind();

I want the value to be the ID of these elements

dimension.DataSource = provider.DimensionList;
dimension.DataValueField = "ID";
dimension.DataBind();

But I don't 开发者_如何学Pythonwant the text to change to the ID, I want that the dimension.DataTextField gets the object.ToString().

How can this be done?


I don't know exactly the type of the DimensionList, but I believe that you could use a select that will return an anonymous type:

dimension.DataSource = provider.DimensionList.Select(d=>new {Id=d.Id,Text=d.ToString()}).ToList();
dimension.DataValueField = "Id";
dimension.DataTextField = "Text";
dimension.DataBind();
0

精彩评论

暂无评论...
验证码 换一张
取 消