2011-9-1
添加ComboBox.items方法:一、数据源绑定,绑定后,items项不能修改,节省代码;二手工添加items方式灵活,代码较多
ComboBox.DisplayMember = "key";ComboBox.ValueMember = "value"; comboBox中的items是一个object类型,与在ASP.net中dropdownlist控件不同(ddl中items是ListItem类型),手工添加items需要创建一个object。ComboBox.Add(new DictionaryEntry(row[strDataTextField].ToString(),row[strDataValueField].ToString()));
DictionaryEntry类
两个构造函数new DictionaryEntry()、new DictionaryEntry(object key,object value)
定义可设置或检索的字典键/值对。
命名空间:System.Collections 程序集:mscorlib(在 mscorlib.dll 中)
示例class Example { public static void Main() { // Create a new hash table. // Hashtable openWith = new Hashtable(); // Add some elements to the hash table. There are no // duplicate keys, but some of the values are duplicates. openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); // When you use foreach to enumerate hash table elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); foreach (DictionaryEntry de in openWith) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); } } }
DropDownStyle属性控制显示给用户的界面。可以输入一个值,该值提供以下功能:简单的下拉列表框(始终显示列表)、下拉列表框(文本部分不可编辑,并且必须选择一个箭头才能查看下拉列表框)或默认下拉列表框(文本部分可编辑,并且用户必须按箭头键才能查看列表)。若要始终显示用户不能编辑的列表,请使用ListBox控件。
如果将DropDownStyle属性设置为DropDownList,则只能从列表中选择有效值。如果将DropDownStyle属性设置为DropDown,则可以在ComboBox的可编辑区域内键入任何值。这种情况下,AutoCompleteMode的Append值与AutoCompleteMode的SuggestAppend值作用相同。