c# - 数据绑定(bind) : 'System.Web.UI.Pair' does not contain a property with the name 'First'

标签 c# asp.net .net vb.net drop-down-menu

我正在将一对列表数据绑定(bind)到下拉列表,由于某种原因它不起作用,我很感兴趣。

我正在使用的代码是:

public void BindDropDown(List<Pair> dataList)
{
    ddlGraphType.DataTextField = "First";
    ddlGraphType.DataValueField = "Second";

    ddlGraphType.DataSource = dataList;
    ddlGraphType.DataBind();
}

我得到了这个异常(exception),这是一个谎言!
DataBinding: 'System.Web.UI.Pair' does not contain a property with the name 'First'.

提前致谢。

已添加

我知道异常的含义,但是 pair 对象确实包含 First 和 Second 属性,这就是问题所在。

最佳答案

FirstSecond是字段而不是 Pair 的属性类型。您需要创建一个具有两个属性的类:

 public class NewPair
 {
    public string First { get; set; }
    public string Second { get; set; }
 }

编辑:使用 Tuple :@Damien_The_Unbeliever 和 @Chris Chilvers 建议
List<Tuple<string, string>> list = new List<Tuple<string, string>>()
 {
   new Tuple<string,string>("One","1"),
   new Tuple<string,string>("Two","2"),
};

ddlGraphType.DataTextField = "Item1";
ddlGraphType.DataValueField = "Item2";

ddlGraphType.DataSource = list;
ddlGraphType.DataBind();

关于c# - 数据绑定(bind) : 'System.Web.UI.Pair' does not contain a property with the name 'First' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257697/

相关文章:

c# - C# 项目列表中的唯一值

c# - WPF 绑定(bind)控件在运行时的宽度

c# - 导出列表<float>到excel文件

asp.net - Signal-R,带或不带 Web API?

.net - 如何使用 nhibernate 更快地插入?

c# - Windows Mobile - Attach on call 开始和录制通话

c# - 使用 C# 抑制 Excel VSTO 中的 "number stored as text"警告

asp.net - 能否将 asp.net WebForm 控件转换为 HTML?

c# - 使用两次时中继器错误

c# - 如何根据新的安全策略在 .Net 中发送电子邮件?