一.数据源和实体,实体类重写tostring方法。
using System.Collections.Generic;
namespace UIPrototype
{
public class DataCommon
{
public static List<DataInfo> GetTimeList()
{
var list = new List<DataInfo>();
var data1 = new DataInfo { Id = "1", Value = "账户余额" };
list.Add(data1);
var data2 = new DataInfo { Id = "2", Value = "账户当日明细" };
list.Add(data2);
var data3 = new DataInfo { Id = "3", Value = "账户历史明细" };
list.Add(data3);
return list;
}
}
public class DataInfo
{
public string Id { get; set; }
public string Value { get; set; }
public override string ToString()
{
return Value;
}
}
}
二.数据绑定
using System;
using System.Windows.Forms;
namespace UIPrototype
{
public partial class QuerySetInfo : Form
{
public QuerySetInfo()
{
InitializeComponent();
}
public void LoadComboBoxEdit()
{
var list = DataCommon.GetTimeList();
foreach (DataInfo item in list)
{
comboBoxEdit1.Properties.Items.Add(item);
}
}
private void QuerySetInfo_Load(object sender, EventArgs e)
{
LoadComboBoxEdit();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
string id = ((DataInfo)comboBoxEdit1.SelectedItem).Id;
string value = ((DataInfo)comboBoxEdit1.SelectedItem).Value;
Console.WriteLine(@"id:" + id + @",value:" + value);
}
}
}
三.结果如下图