将数据源转换为对象

时间:2013-09-14 23:38:21

标签: c# winforms data-binding

我有课

public class CLASS_K
{
    public static List<AA> simpleList = new List<AA>()
    {
        new AA(){NNN = "o1", NNN2 = "o1a"},
        new AA(){NNN = "o2", NNN2 = "o2a"}
    };

    public class AA
    {
        public string NNN { get; set; }
        public string NNN2 { get; set; }
    }
}

我为label设置了绑定:

label1.DataBindings.Add("text", CLASS_K.simpleList[1], "NNN");

如何将dataSource从标签转换为AA类?

我的错误想法:

BindingSource binding = (BindingSource)label1.DataBindings[0].DataSource;
CLASS_K.AA b = (CLASS_K.AA) binding.DataSource;

1 个答案:

答案 0 :(得分:0)

DataSource包含CLASS_K.AA类型的对象,请尝试以下操作:

if (label1.DataBindings.Count != 0)
{
    var b = label1.DataBindings[0].DataSource as CLASS_K.AA;
    if (b != null)
    {
        // do something...
    }
}