我怎么能在radiobuttonlist的DataTextField中给出两个字符串?

时间:2013-03-06 19:04:13

标签: c#

这里“name”和“specification”是我的表的字段,我想在DataTextField中显示这两个字段。我试过上面的代码:

RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = "name";
RadioButtonList1.DataTextField = "specification";
RadioButtonList1.DataBind(); 

但它没有用。

2 个答案:

答案 0 :(得分:1)

RadioButtonList1.Items.Clear();

foreach(var row in dt.Rows)
{
    var txt = row["name"].ToString() + " " + row["specification"].ToString();
    var val = row["id"].ToString();
    var item = new ListItem(txt,val);
    RadioButtonList1.Items.Add(item);
}

答案 1 :(得分:1)

您需要填充项目数据绑定事件的字段或从dt创建新的组合列;

var newDT = (from r in dt
             select new 
             {
                 ID = r.ID,
                 NameAndSpec = r.name + ", " + r.specification
             }
             );