XtraEditors二、ComboBox、ComboBoxEdit、CheckedComboBoxEdit

📝 ✏️ 📌
XtraEditors二、ComboBox、ComboBoxEdit、CheckedComboBoxEdit

https://documentation.devexpress.com/WindowsForms/DevExpress.XtraEditors.ComboBoxEdit.class

1、使用Winform自带ComboBox控件,可直接绑定数据源

因为Winform自带 ComboBox 是有 DataSource 属性的,所以它可以直接绑定数据源,如 DataTable、ListItem 等。使用 DataTable 或List直接绑定。

public void BindSource()

{

DataTable dt = new DataTable();

dt.Columns.Add("Text", Type.GetType("System.String"));

dt.Columns.Add("Value", Type.GetType("System.String"));

dt.Rows.Add("请选择", "0");

dt.Rows.Add("选项一", "1");

dt.Rows.Add("选项二", "2");

dt.Rows.Add("选项三", "3");

comboBox1.DataSource = dt;

comboBox1.DisplayMember = "Text"; // Text,即显式的文本

comboBox1.ValueMember = "Value"; // Value,即实际的值

comboBox1.SelectedIndex = 0; // 设置为默认选中第一个

}

string text = this.comboBox1.Text; //获取选中项文本

string value = this.comboBox1.SelectedValue.ToString(); //获取选中项的值

2、ComboBoxEdit:不能直接绑定数据源,只能手工添加。

使用 ComboBoxEdit 控件添加key/value项。

因为 ComboBoxEdit 没有 DataSource 属性,所以不能直接绑定数据源,只能一项一项的添加。

public class ListItem : Object

{

public string Text { get; set; }

public string Value { get; set; }

public ListItem(string text,string value)

{

this.Text = text;

this.Value = value;

}

public override string ToString()

{

return this.Text;

}

}

public void BindSource()

{

string text = string.Empty;

string value = string.Empty;

ListItem item = null;

for (int i = 0; i < 4; i++)

{

if (i==0)

{

text = "请选择";

}

else

{

text = "选项" + i.ToString();

}

value = i.ToString();

item = new ListItem(text, value);

this.comboBoxEdit1.Properties.Items.Add(item);

}

}

获取选中项的值时,注意判断是否选择。

string text = string.Empty;

string value = string.Empty;

if (comboBoxEdit1.SelectedIndex < 0) //小于0,表示未选择,如果是输入的也小于0

{

text = comboBoxEdit1.Text.Trim(); //只能获取输入的文本

}

else

{

text= (comboBoxEdit1.SelectedItem as ListItem).Text; //获取选中项文本

value = (comboBoxEdit1.SelectedItem as ListItem).Value; //获取选中项的值

}

3、CheckedComboBoxEdit:可直接绑定数据源

//设置数据源

checkedComboBoxEdit1.Properties.DataSource = Facility.GetList(factoryID);

checkedComboBoxEdit1.Properties.DisplayMember = "Name";

checkedComboBoxEdit1.Properties.ValueMember = "ID";

//设置选中项

checkedComboBoxEdit1.EditValue = strid

//得到选择项的ID字符串(逗号分隔)

object items = checkedComboBoxEdit1.Properties.GetCheckedItems();

checkedComboBoxEdit1.SetEditValue(string.Empty);//重置

checkedComboBoxEdit1.Properties.Items.Clear();//清空数据源

//循环项

foreach (CheckedListBoxItem item in checkedComboBoxEdit1.Properties.Items)

{

item.CheckState = CheckState.Unchecked;

}

//手工绑定

for (int i = 0; i < myList.Count; i++)

{

if (myList[i].isCheck == true)

checkedComboBoxEdit1.Properties.Items.Add(i, myList[i].Name, CheckState.Checked, true);

else

checkedComboBoxEdit1.Properties.Items.Add(i, myList[i].Name, CheckState.Unchecked, true);

}

🔗 相关推荐

✨ 💡 🎯
[其他]黑暗!国内BAT三巨头才是最大的流氓!
开彩365下载安装

[其他]黑暗!国内BAT三巨头才是最大的流氓!

📅 09-15 👀 7430
定制一个软件要多少钱?一文带你看明白
开彩365下载安装

定制一个软件要多少钱?一文带你看明白

📅 08-05 👀 3879
部落冲突部落怎么快速招人 部落冲突部落快速招人方法介绍