浅析C# 绑定ComBobox控件的解决方法

开发 后端
这里将介绍C# 绑定ComBobox控件的解决方法,包括会出现的问题,希望对大家了解C# 绑定有所了解。

一般的C# 绑定,都是将ID进行递归,然后再帮顶。这里将解决普通绑定中会出现的问题。希望对大家有所帮助。

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

ID    

NAME

ParentID

1 aa 0
2 bb 1
3 cc 2

从以上图标中可看出关系:ID:1是父节点,ID:2是ID:1的子节点,而ID:3又是ID:2的子节点。这种关系其实并不复杂,但是你要通过一个Combobox来检索出ID:1节点下的所有节点,就必须使用递归了。因此,问题也就出现了,用普遍的方式来C# 绑定,会使界面上数量显示成[ 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.     } 

C# 绑定方法:

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

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

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

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. // 调用方法   
  17. int indext = com(this.treeViewer.SelectedNode.Tag.ToString());   
  18. cboCategory.SelectedItem = cboCategory.Items[indext]; 

【编辑推荐】

  1. C# Attribute的概念与使用浅析
  2. C# AttributeUsage的使用浅析
  3. 浅析Attribute在C# WinForm控件开发中的使用
  4. 浅谈C#控件属性串行化的实现
  5. C#实例详解TypeConverterAttribute应用
责任编辑:彭凡 来源: CSDN
相关推荐

2009-09-15 17:46:08

C#绑定句柄无效

2009-09-15 15:40:25

C# 绑定

2011-04-25 15:15:00

C#

2009-09-08 14:54:40

C# listBox控

2009-08-28 16:31:21

C# treeview

2009-08-11 14:45:41

C# DataGrid

2009-08-06 16:58:40

C#编写ActiveX

2009-09-04 17:58:38

C# Web Brow

2009-08-12 10:35:50

C#调用ActiveX

2009-08-28 15:05:35

C#编写Calenda

2009-08-26 13:36:33

C#打印控件

2009-08-13 15:40:29

C#数据绑定控件

2009-09-11 09:15:06

C# get方法

2009-08-20 16:15:19

C# 匿名方法

2009-09-10 14:52:55

C# get

2009-08-10 17:36:17

C#扩展方法

2009-09-11 10:41:20

C# WinForm控

2009-09-07 09:36:29

C# DisposeDispose方法

2009-08-28 15:52:23

C#利用sharpzi

2009-08-13 10:40:15

C#读取Excel
点赞
收藏

51CTO技术栈公众号