编写winform(C#.net)程序中使用下拉框控件时,无法指定选择项的解决办法
首先定义类
  public  class ItemListHelp
    {
       public string text { get; set; }
       public string vlaue { get; set; }
    }

然后绑定ComboBox控件
  DataTable ta=GetAllFieldByLetterType();
            if(ta!=null)
            {
                drop_field.Items.Clear();
              
               
                foreach(DataRow row in ta.Rows)
                {
                    ItemListHelp item1=new ItemListHelp();
                    item1.text=row["fieldname"].ToString().Trim();
                    item1.vlaue=row["typename"].ToString().Trim();
                    if(item1.text!="")
                    drop_field.Items.Add(item1);
                }
                drop_field.DisplayMember = "text";
                drop_field.ValueMember = "value";
                //使第一项不为空
                drop_field.SelectedIndex = 0;
            }
这样绑定后ComcoBox的每一项都与ItemListHelp类相关联
设定选择项
//selectitem为要设定为选择项的显示文本
  if (selectitem != "")
            {
                foreach (ItemListHelp item in drop_field.Items)
                {
                    if (item.text == selectitem)
                    {
                        drop_field.SelectedItem = item;
                        break;
                    }
                }

            }

posted on 2011-04-20 18:28  dezi  阅读(1084)  评论(0编辑  收藏  举报