zoukankan      html  css  js  c++  java
  • Winform中ComcoBox控件设置选定项

    编写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;
                        }
                    }

                }

  • 相关阅读:
    c++ stl string char* 向 string 转换的问题
    不要在疲惫中工作
    今天
    悠然自得
    忙与闲
    <转>LuaTinker的bug和缺陷
    匿名管道
    SetWindowHookEx 做消息响应
    最近工作
    实现网页页面跳转的几种方法(meta标签、js实现、php实现)
  • 原文地址:https://www.cnblogs.com/ldqwyl/p/2022625.html
Copyright © 2011-2022 走看看