仅更改列表框的DataValueField?

时间:2011-02-24 13:58:55

标签: c# asp.net listbox

我有Listbox

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

我希望该值为这些元素的ID

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

但我不希望文字更改为ID,我希望dimension.DataTextField获得object.ToString()

如何做到这一点?

1 个答案:

答案 0 :(得分:4)

我不确切知道DimensionList的类型,但我相信您可以使用将返回匿名类型的select:

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