必须是Winform的
由于从来没有进行过Winform的开发,所以不知道从哪里入手。问题比较清晰,如下:
有一个产品列表需要放到ComboBox控件中,并且会在很多页面中出现。所以需要封装一个ComboBox控件,在控件内部进行数据局绑定,无需在调用的页面上进行数据源赋值操作(在其他页面直接拖过去就可以用了)。
假如控件的名字叫BrandComboBox
用下面的数据进行绑定            DataTable tempDt = new DataTable();
            tempDt.Columns.Add("ItemText");
            tempDt.Columns.Add("ItemValue");
            for (int i = 0; i < 5; i++)
            {
                DataRow tempDr = tempDt.NewRow();
                tempDr["ItemText"] = String.Format("{0}{0}{0}{0}", i);
                tempDr["ItemValue"] = i.ToString();
                tempDt.Rows.Add(tempDr);
            }

解决方案 »

  1.   

    时间久了发现,派生自己的Control不如给Control写一个Helper来操作它。
      

  2.   

    你的DataTable构造时,列的属性只增加了名字,其他的比如类型System.Int32还是其他也需要设置一下吧。
      

  3.   

    this.comboBox1.DisplayMember = "作为现实文本的字段或属性名";//显示的信息 
    this.comboBox1.ValueMember = "作为值读取的字段或属性名";//Value 
    this.comboBox1.DataSource=数据源; 
     public MyComBase ()//构造函数绑定
            {
                
            }
      

  4.   

    做一个用户控件。然后在用户控件里面放个Combobox,在设置this.comboBox1.DisplayMember = "作为现实文本的字段或属性名";//显示的信息
    this.comboBox1.ValueMember = "作为值读取的字段或属性名";//Value
    this.comboBox1.DataSource=数据源;