C# 绑定ComBobox控件

在C/S模式下,在这样一种情况下:

ID    

NAME

ParentID

1aa0
2bb1
3cc2

从以上图标中可看出关系:ID:1是父节点,ID:2是ID:1的子节点,而ID:3又是ID:2的子节点。这种关系其实并不复杂,但是你要通过一个Combobox来检索出ID:1节点下的所有节点,就必须使用递归了。因此,问题也就出现了,用普遍的方式来绑定,会使界面上数量显示成[ Control:name ] ,具体的笔者不再重复,如果你看到这篇文章,那么你应该会明白我的意思。好,下面来看解决方案吧!

 

写一个ListItem类:

  1. public class ListItem
  2.     {
  3.         private string id = string.Empty;
  4.         private string name = string.Empty;
  5.         public ListItem(string sid, string sname)
  6.         {
  7.             id = sid;
  8.             name = sname;
  9.         }
  10.         public override string ToString()
  11.         {
  12.             return this.name;
  13.         }
  14.         public string ID
  15.         {
  16.             get
  17.             {
  18.                 return this.id;
  19.             }
  20.             set
  21.             {
  22.                 this.id = value;
  23.             }
  24.         }
  25.         public string Name
  26.         {
  27.             get
  28.             {
  29.                 return this.name;
  30.             }
  31.             set
  32.             {
  33.                 this.name = value;
  34.             }
  35.         }
  36.     }

 

绑定方法:

  1.         //定义一个方法,传两个参数;
  2.         // 注意:参数得看你的具体需要,笔下这个方法仅作参考;
  3.         void init(ComboBox cb, int parentId)
  4.         {
  5.             string commandText = String.Format("Select * from  tb_SystemCategory where parentID= {0}",parentId);
  6.             DataSet ds = globalBLL.GetList(commandText);
  7.             if (ds != null)
  8.             {
  9.                 foreach (DataRow dr in ds.Tables[0].Rows)
  10.                 {
  11.                     Global.ListItem item = new LMInvoicingCS.Global.ListItem(dr["id"].ToString(),dr["name"].ToString());
  12.                     cb.Items.Add(item);
  13.                     init(cb, int.Parse(dr["id"].ToString()));
  14.                 }
  15.                 ds = null;
  16.             }
  17.         }

基本上到这里就可以了,以下是我在“添加/修改”时作的一些设置。

; 添加的时候,绑定完控件,显示的应该是第一条数据,因此:

  1. cboCategory.SelectedItem = cboCategory.Items[0];

;修改一条数据,绑定完控件,显示给客户的绑定项应该是该编辑项的category,因此:

  1. // 定义一个方法; 
  2. // 这个方法的效率不高,属于老土型,如果有哪位朋友有更好的方案,欢迎交流,谢谢! 
  3.                 private int com(string value)
  4.                 {
  5.                     int index = -1;
  6.                     for (int i = 0; i < rca.cboCategory.Items.Count; i++)
  7.                     {
  8.                         if (((ListItem)rca.cboCategory.Items[i]).ID == value)
  9.                         {
  10.                             index = i;
  11.                         }
  12.                     }
  13.                     return index;
  14.                 }
  15. // 调用方法
  16. int indext = com(this.treeViewer.SelectedNode.Tag.ToString());
  17. cboCategory.SelectedItem = cboCategory.Items[indext];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值