C#组合框使用文本设置值 [英] c# combobox set the value using text

查看:52
本文介绍了C#组合框使用文本设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以这种方式填写的组合框 farmRegion

I have a combobox farmRegion that I fill in this way

private void fillRegionData() {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Description", typeof(string));
            farmRegion.ValueMember = "ID";
            farmRegion.DisplayMember = "Description";
            farmRegion.SelectedValue = "ID";
            for (int i = 0; i < StaticData.RegionNames.Count; i++)
            {
                dt.Rows.Add(StaticData.RegionValues[i], StaticData.RegionNames[i]);
            }
            farmRegion.DataSource = dt;
        }

其中 StaticData.RegionNames 是:

public static List<string> RegionNames = new List<string>() { "Select Region", "EASTERN", "WESTERN", "NORTHERN", "EASTERN2", "NORTHERN", "MIDDLE" };

StaticDate.RegionValues

public static List<string> RegionValues = new List<string>() { "-10", "1", "2", "3", "4", "5", "6" };

保存表单时,我保存的是组合框的文本,而不是(这是必需的问题).

when I save the form, I save the text of the combobox, not the value (this is a requirement issue).

现在我想再次重新加载comboxbox.我已经知道了文本,但是我需要使组合框触发,并且已经选择了选项文本.

now I want to reload the comboxbox again. I already know the text but I need to make the combobox fires and the option text is already selected.

我尝试过:

farmRegion.Text = myText

但仍然选择了第一个选项.

but still the first option is selected.

推荐答案

在设置文本 farmRegion.Text = myText 之前,请先设置一个断点并检查 combobox 数据源,并确保在 combobox 中存在 myText .

Before setting the text farmRegion.Text = myText put a break point and checks the combobox datasource, and ensure myText is present in combobox.

如果您处理了组合框的任何事件,请在该事件上设置一个断点,并检查执行 farmRegion.Text = myText 语句后发生了什么.

If you handled any events of combobox put a break point on that events and check what happend after the execution of farmRegion.Text = myText statement.

这两个步骤不能解决您的问题,然后找出您的文本值的索引为

These two steps doen't solve your issue then find out the index of your text value as

int index = farmRegion.FindString(myText);
farmRegion.SelectedIndex = index;

这篇关于C#组合框使用文本设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆