手动写入一个combobox控件:
comboBox1.Items.Insert (0,"请选择");
comboBox1.Items.Insert (1,"根目录");
comboBox1.SelectedIndex=0;我想取出该控件的SelectedValue,可是提示末将对象引用设置到对象的实例,这说明写入的时候没有指定SelectedValue,那么该怎么样指定呢?多谢了
comboBox1.Items.Insert (0,"请选择");
comboBox1.Items.Insert (1,"根目录");
comboBox1.SelectedIndex=0;我想取出该控件的SelectedValue,可是提示末将对象引用设置到对象的实例,这说明写入的时候没有指定SelectedValue,那么该怎么样指定呢?多谢了
ArrayList cbValues = new ArrayList();//定义到外面private ArrayList cbValues = new ArrayList();
this.comboBox1.Items.Insert(0, "");
cbValues.Insert(0, value0);
//取Value的话就去cbValues取
string sql1="select id,name from addressmanage where type='2'";
DataTable dt1=DB.DataAccess.GetSqlData (sql1);
comboBox2.ValueMember = "id";
...........取的时候直接取comboBox2.SelectedValue就行了如果不是从数据库绑定的话,是手写的话,就不能用comboBox2.ValueMember这样的方式设置了吗?
看看MSDN
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
[align=left]dt.Columns.Add("name",typeof(string)) ;
dt.Columns.Add("id",typeof(int)) ; DataRow row ; row = dt.NewRow() ;
row["id"] = (int)ZhiLingDanType.TuoYunJianBaoZhuangDan ;
row["name"] = "托运件包装单" ;
dt.Rows.Add(row) ; row = dt.NewRow() ;
row["id"] = (int)ZhiLingDanType.PaiSongChuKuDan ;
row["name"] = "派送出库单" ;
dt.Rows.Add(row) ;
this.DataSource = dt ;
this.DisplayMember = "name" ;
this.ValueMember = "id" ; this.SelectedValue = selectedID ;
comboBox1.Items.Insert(0, new ComboBoxItem("0","请选择"));
comboBox1.Items.Insert(1, new ComboBoxItem("1","根目录"));
comboBox1.SelectedIndex = 1; MessageBox.Show(comboBox1.SelectedItem.ToString());//根目录
MessageBox.Show(((ComboBoxItem)comboBox1.SelectedItem).Value);//1} internal class ComboBoxItem:Object
{
private string value;
private string text;
public ComboBoxItem(string _value, string _text)
{
value = _value;
text = _text;
} public string Text
{
get { return text; }
} public string Value
{
get { return value; }
} public override string ToString()
{
return text;
} }