如何从C#中的组合框获取特定值? [英] how to fetch particular Value from Combobox in C#?
本文介绍了如何从C#中的组合框获取特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表单中有2个组合框.
当我从combobox2中选择任何值时,我需要设置
的索引
基于combobox2的选定值的combobox1.
因此,我编写了如下代码:
Hi,
I have 2 Comboboxes in my form.
When I select any value from combobox2, I need to set the index of
combobox1 based on the selectedvalue of combobox2.
So, I wrote the code like:
public void SetDropDownIndex(System.Windows.Forms.ComboBox DdList, string CellText)
{
int i;
for (i = 0; i <= DdList.Items.Count - 1; i++)
{
//if (DdList.Items[i].ToString() == CellText.ToString())
if (DdList.Text == CellText.ToString())
{
DdList.SelectedIndex = i;
break;
}
}
//return i;
}
CellText-这是Combobox2.Text(选中的项目)
当我检查Ddlist Count时,它显示得很好,但是如果我要访问Ddlist.Items [0] .Tostring(),它会给我System.Data.Datarow.
CellText- This is Combobox2.Text (The Item which is selected)
When I check Ddlist Count it is showing perfectly,but if I want to access Ddlist.Items[0].Tostring(), it gives me System.Data.Datarow.
推荐答案
尝试一下:
Try this:
int index = DdList.FindExactString(CellText.ToString());
if (index >= 0)
{
DdList.SelectedIndex = index;
}
else
{
// handle the fact that DdList doesn''t contain the string
}
尝试一下
Try this
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Items.Contains(comboBox2.Text))
{
comboBox1.SelectedIndex= comboBox1.Items.IndexOf(comboBox2.Text);
}
}
这篇关于如何从C#中的组合框获取特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文