1程序用到了第三方的DevExpress控件
2属性
Code
private List<TradeTypeEntity> trades = null;//存储数据
private List<LookUpEdit> ctl_dic = new List<LookUpEdit>();//存储控件
private TradeTypeEntity empty_model = new TradeTypeEntity() { PT_TradeID = null, PT_TradeName = "请选择" };//空数据的示例
3构造函数
Code
/// <summary>
/// 加载行业类型(仅父类)
/// </summary>
public void InitTrade()
{
trades = CacheStrategy.Instance.GetObject(CacheKey.Trades) as List<TradeTypeEntity>;
var data = (from v in trades where v.PT_ParentID.Equals("0") select v).ToList();
LookUpEdit ctl = PrepareCtl(data);
ctl.EditValue = null;
ctl.EditValueChanged += new EventHandler(ctl_EditValueChanged);
ctl_dic.Insert(0, ctl);
ShowCtls();
}
/// <summary>
/// 加载行业类型
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void InitTrade(string trade_code)
{
trades = CacheStrategy.Instance.GetObject(CacheKey.Trades) as List<TradeTypeEntity>;
var target = trades.Single(v => { return v.PT_TradeCode.Equals(trade_code); });
CreateCtls(target);
ShowCtls();
}
4创建控件
Code
/// <summary>
/// 创建控件,用到了递归
/// </summary>
/// <param name="trade_code"></param>
private void CreateCtls(TradeTypeEntity target)
{
var data = (from v in trades where v.PT_ParentID.Equals(target.PT_ParentID) select v).ToList();
LookUpEdit ctl = PrepareCtl(data);
ctl.EditValue = target.PT_TradeID;
ctl.EditValueChanged += new EventHandler(ctl_EditValueChanged);
ctl_dic.Insert(0, ctl);
if (target.PT_ParentID.Equals("0"))
{
return;
}
var target_temp = trades.Single(v => { return v.PT_TradeID.Equals(target.PT_ParentID); });
CreateCtls(target_temp);
}
5准备控件
Code
/// <summary>
/// 准备控件
/// </summary>
/// <param name="ctl"></param>
/// <param name="data"></param>
private LookUpEdit PrepareCtl(List<TradeTypeEntity> data)
{
data.Insert(0, empty_model);
LookUpEdit ctl = new LookUpEdit();
ctl.Properties.PopupWidth = 50;
ctl.Properties.DataSource = data;
ctl.Properties.ShowFooter = false;
ctl.Properties.ShowHeader = false;
ctl.Properties.PopupFormMinSize = new System.Drawing.Size(50, 0);
ctl.Properties.DisplayMember = "PT_TradeName";
ctl.Properties.ValueMember = "PT_TradeID";
ctl.Properties.Columns.Add(new LookUpColumnInfo("PT_TradeName"));
ctl.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
ctl.Properties.NullText = "请选择";
return ctl;
}
6控件选择项变更的事件
Code
/// <summary>
/// 选择项变更事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ctl_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit sourceCtl = (LookUpEdit)sender;
int index = ctl_dic.IndexOf(sourceCtl);
ctl_dic.RemoveRange(index + 1, ctl_dic.Count - index - 1);
if (sourceCtl.EditValue == null)
{
ShowCtls();
return;
}
var data = (from v in trades where v.PT_ParentID.Equals(sourceCtl.EditValue) select v).ToList();
if (data.Count > 0)
{
LookUpEdit ctl = PrepareCtl(data);
ctl.EditValue = null;
ctl.EditValueChanged += new EventHandler(ctl_EditValueChanged);
ctl_dic.Add(ctl);
}
ShowCtls();
}
7显示控件
Code
/// <summary>
/// 显示控件
/// </summary>
private void ShowCtls()
{
ctlTable.Controls.Clear();
for (int i = 0; i < ctl_dic.Count; i++)
{
ctlTable.Controls.Add(ctl_dic[i], i, 0);
}
}
8获取选中的值和文本
Code
/// <summary>
/// 获取选中的值
/// </summary>
/// <returns></returns>
public string GetSelectedID()
{
if (ctl_dic.First().EditValue == null)
{
return string.Empty;
}
else if (ctl_dic.Last().EditValue == null)
{
return ctl_dic[ctl_dic.Count - 2].EditValue as string;
}
return ctl_dic.Last().EditValue as string;
}
/// <summary>
/// 获取选中的文本
/// </summary>
/// <returns></returns>
public string GetSelectedText()
{
if (ctl_dic.First().Text.Equals("请选择"))
{
return string.Empty;
}
else if (ctl_dic.Last().Text.Equals("请选择"))
{
return ctl_dic[ctl_dic.Count - 2].Text;
}
return ctl_dic.Last().Text;
}
至此完成
有机会在VS2008里不用任何第三方皮肤和控件再做一遍分享给网友吧