C# WinForm 中ComboBox数据绑定的问题
怎样让WinForm中的ComboBox显示表中的一个字段,同时又绑定另一个字段?
在Web中的ComboBox这样写可以绑定两个值:
this.ComboBox1.DataTextField="B000602";//显示中文,方便用户选择
this.ComboBox1.DataValueField="B000601";//绑定与选择对应的另一个值
this.ComboBox1.DataBind();
但是在WinForm程序中该怎么写啊?
0******************************************************************
DataSet ds = new DataSet();//这个DataSet是你从数据库里取出来的值
string[] arr = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = ds.Tables[0].Rows[i][2].ToString();
}
textBox1.AutoCompleteCustomSource.AddRange(arr);
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
1.*******************************************************************
假设combobox绑定的列表为DataSet2的ListTable表(含有ListID, ListName字段),需要绑定的记录字段为DataSet1的Table1表的ListID字段
combobox.DataSource = dataset2.Tables["ListTable"];
combobox.DisplayMember = "ListName";
combobox.ValueMember = "ListID";
combobox.DataBindings.Add("SelectedValue", dataset1, "Table1.ListID");
2.*****************************************************************
//dt为数据表,ID,Name为dt的两个字段:
comboBox1.DataSource = dt ;
comboBox1.ValueMember ="ID";
comboBox1.DisplayMember ="Name";
3.******************************************************************
SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind");
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "Select * from Customers where country='USA'";
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds, "Customers");
comboBox1.DataSource = ds.Tables["Customers"];
comboBox1.DisplayMember = "CompanyName";
comboBox1.ValueMember = "CompanyName";
++++++++++++++++或者++++++++++++++++++++++
SqlConnection con = new SqlConnection("server=192.168.2.198;uid=sa;pwd=sa;database=northwind");
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "Select * from Customers where country='USA'";
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds, "Customers");
comboBox1.DataSource = ds;
comboBox1.DisplayMember = "Customers.CompanyName";
comboBox1.ValueMember = "Customers.CompanyName";
++++++++++++往DataGrid里添加下拉列表++++++++++++
DataGridTableStyle dgt = new DataGridTableStyle();
dgt.MappingName = "test";
DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn();
dgtbc.MappingName = "name";
dgtbc.HeaderText= "name";
ComboBox cmbFunctionArea = new ComboBox();
cmbFunctionArea.DataSource = DtGeneral;
cmbFunctionArea.DisplayMember = "name";
cmbFunctionArea.ValueMember = "value";
cmbFunctionArea.Cursor = Cursors.Arrow;
cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;
cmbFunctionArea.Dock = DockStyle.Fill;
dgtbc.TextBox.Controls.Add(cmbFunctionArea);
dgt.GridColumnStyles.Add(dgtbc);
cmbFunctionArea.SelectionChangeCommitted +=new EventHandler(cmbFunctionArea_SelectionChangeCommitted);
+++++++++++++修改++++++++++++++++
private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
{
((DataTable)dataGrid1.DataSource).Rows[dataGrid1.CurrentRowIndex][0] = ((ComboBox)sender).Text.ToString();
dataGrid1.SetDataBinding(DtGeneral,null);
}
4.************************************************************************
DataBindings是一般控件所具有的,是绑定数据源的某一个字段
combobox.DataBindings.Add("要绑定控件的属性如下拉框的SelectedValue\Text", 数据源如dataset1, "导航路径如Table1.ListID");
但是,DataBindings只能绑定一个字段,而绑定多个字段时典型的如列表控件Combobox、ListBox控件,需要键值对,这时就需要指定DataSource(实现IList接口就行),然后指定ValueMember、DisplayMember 。
所以,如果是下拉列表,你只想绑定一个字段,用DataBindings可以,想绑定两个字段
用combobox.DataSource = 数据源;
combobox.DisplayMember = 对应显示字段名;
combobox.ValueMember = 对应存储字段名;
不要同时用
5.*************************************************************************
绑定以后就可以了,读取选中信息 :
object val = combobox.SelectedValue;
string txt = combobox.Text;
SelectedIndex、SelectedItem表示选中了哪项目,也可以设置combobox.SelectedIndex = 0 或
直接设置 combobox.SelectedValue = "一个已存在的值";或 combobox.Text = "一个已存在的文本";
如果是绑定的,一定要设置的是一个在绑定在已存在的项。