如何获取绑定数据的combobox的选中值及选中text
1个回答
展开全部
1、绑定combobox的代码如下:
DataTable LDT_CodeType = Models.MoCodefile.GetCodeType();
if (LDT_CodeType != null)
{
cboxCtype.DataSource = LDT_CodeType;
cboxCtype.DisplayMember = "codetypename";
cboxCtype.ValueMember = "codetypeno";
}
获取选中值:cboxCtype.SelectedIndex.ToString() 或是 cboxCtype.SelectedValue.ToString()
获取选中text:
方法一:DataRowView dr = (DataRowView)cboxCtype.Items[cboxCtype.SelectedIndex];
方法二:cboxCtype.GetItemText(cboxCtype.Items[cboxCtype.SelectedIndex])
2、C#中ComboBox能否设定多个ValueMenber?
举个例子:表student,有字段id,name,tel有个combobox绑定了这个表,其中DisplayMember设置为id,现要取出对应的name和tel, 用ValueMember只能取一个,该怎么做呀?
一般是这样绑定和取值: comboBox1.DataSource = student; //绑定student表为数据源comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "id"; string name = comboBox1.SelectedText;//取DisplayMember值 string id = comboBox1.SelectedValue.ToString();//取ValueMember值 DataTable table1 = this.comboBox1.DataSource as DataTable; int rowIndex = this.comboBox1.SelectedIndex; string str1 = table1.Rows[rowIndex]["tel"].ToString();//取绑定表中的其它值
DataTable LDT_CodeType = Models.MoCodefile.GetCodeType();
if (LDT_CodeType != null)
{
cboxCtype.DataSource = LDT_CodeType;
cboxCtype.DisplayMember = "codetypename";
cboxCtype.ValueMember = "codetypeno";
}
获取选中值:cboxCtype.SelectedIndex.ToString() 或是 cboxCtype.SelectedValue.ToString()
获取选中text:
方法一:DataRowView dr = (DataRowView)cboxCtype.Items[cboxCtype.SelectedIndex];
方法二:cboxCtype.GetItemText(cboxCtype.Items[cboxCtype.SelectedIndex])
2、C#中ComboBox能否设定多个ValueMenber?
举个例子:表student,有字段id,name,tel有个combobox绑定了这个表,其中DisplayMember设置为id,现要取出对应的name和tel, 用ValueMember只能取一个,该怎么做呀?
一般是这样绑定和取值: comboBox1.DataSource = student; //绑定student表为数据源comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "id"; string name = comboBox1.SelectedText;//取DisplayMember值 string id = comboBox1.SelectedValue.ToString();//取ValueMember值 DataTable table1 = this.comboBox1.DataSource as DataTable; int rowIndex = this.comboBox1.SelectedIndex; string str1 = table1.Rows[rowIndex]["tel"].ToString();//取绑定表中的其它值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询