WinForm下ComboBox设定SelectedValue总结
最近在项目中处理一个winForm中设定ComboBox值,奇怪的是用一个对象匹配ComboBox的Datasource集合的一个对象时,设置SelectedValue时,竟然得到SelectedValue为null。
网上搜索一下,有说是如果用DateSet绑定时,SelectedValue得到的是null或DataRowView。
后来反复实践,发现可以用以下方法实现:
private void SetValueByItem()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.SelectedItem = ac.FindAll(delegate(Area ar) { return ar.Area_ID == a.Area_ID; })[0];
lbResult.Text = cbList.SelectedItem.GetType().ToString() + ":" + cbList.SelectedValue.ToString();
}
其他两种设置方式:
private void SetValueByValue()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.SelectedValue = a.Area_ID;
lbResult.Text = cbList.SelectedValue.GetType().ToString() + ":" + cbList.SelectedValue.ToString();
}
private void SetValueByText()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.SelectedIndex = cbList.FindString(a.Area_Name);
lbResult.Text = cbList.SelectedValue.GetType().ToString() + ":" + cbList.SelectedValue.ToString();
}
完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinFormCombobox
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
#region Members
AreaLists ac;
public AreaLists GetTestArea()
{
AreaLists al = new AreaLists();
for (int i = 1; i < 11; i++)
{
Area a = new Area();
a.Area_ID = i.ToString();
a.Area_Name = "第" + i.ToString() + "名";
al.Add(a);
}
return al;
}
#endregion
#region Events
private void frmMain_Load(object sender, EventArgs e)
{
ac = GetTestArea();
cbList.DataSource = ac;
cbList.DisplayMember = "Area_Name";
cbList.ValueMember = "Area_ID";
}
private void btnSetCombobox_Click(object sender, EventArgs e)
{
////SetValue0();
//SetValueByValue();
////SetValueByItem();
SetValueByText();
}
#endregion
#region Methods
private void SetValue0()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.DataSource = GetTestArea();
cbList.DisplayMember = "Area_Name";
cbList.ValueMember = "Area_ID";
cbList.SelectedValue = a.Area_ID;
lbResult.Text = cbList.SelectedValue.ToString();
}
private void SetValueByValue()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.SelectedValue = a.Area_ID;
lbResult.Text = cbList.SelectedValue.GetType().ToString() + ":" + cbList.SelectedValue.ToString();
}
private void SetValueByItem()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.SelectedItem = ac.FindAll(delegate(Area ar) { return ar.Area_ID == a.Area_ID; })[0];
lbResult.Text = cbList.SelectedItem.GetType().ToString() + ":" + cbList.SelectedValue.ToString();
}
private void SetValueByText()
{
Area a = new Area();
a.Area_ID = "8";
a.Area_Name = "第8名";
cbList.SelectedIndex = cbList.FindString(a.Area_Name);
lbResult.Text = cbList.SelectedValue.GetType().ToString() + ":" + cbList.SelectedValue.ToString();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace WinFormCombobox
{
#region Area
public class Area
{
private string m_Area_ID;
public string Area_ID
{
get { return m_Area_ID; }
set { m_Area_ID = value; }
}
private string m_Area_Name;
public string Area_Name
{
get { return m_Area_Name; }
set { m_Area_Name = value; }
}
private double m_Area_Order;
public double Area_Order
{
get { return m_Area_Order; }
set { m_Area_Order = value; }
}
}
#endregion
[Serializable]
public class AreaLists : List<Area>
{
private int _maxItems = 0;
public int MaxItems { get { return this._maxItems; } set { this._maxItems = value; } }
}
}