2013-03-06 22 views
1

這裏的「名稱」和「規格」是我的表的字段,我想在DataTextField中顯示這​​兩個字段。我試過上面的代碼:我如何能夠在radiobuttonlist的DataTextField中給出兩個字符串?

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

但它不工作。

+1

[在DataTextField中組合兩個字段。這是可能的嗎?](http://stackoverflow.com/questions/4217481/combining-two-fields-in-a-datatextfield-is-this-possible) – 2013-03-06 19:08:21

回答

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); 
} 
+0

+1或者也可以。 – Narnian 2013-03-06 19:09:17

1

您將需要在項目數據綁定事件上填充字段或從dt創建新的組合列;

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

我喜歡這個解決方案。 – 2013-03-06 19:13:10

相關問題