我有一个组合框,我用格式填写表中的数据"代码:值"
稍后我定义它,例如:
this.cmdColor.DataSource = GetValuesForCombo("COLOR");
this.cmdColor.DisplayMember = "DESCR";
this.cmdColor.ValueMember = "CODE";
this.cmdColor.SelectedIndex = -1;
通过这种方式,我进入组合框" 1:绿色"," 2:红色"等等 现在我想从数据网格中选择组合值。如果我有巫婆的列颜色我有"绿色"在另一行"红色"等,当我点击列颜色为红色值的行时,我需要在组合文本中显示" 2:红色"我尝试使用查找字符串命令,但只能使用代码(如果我在列中使用颜色编写2,我将在组合框中选择值,但如果我写"红色"我不会。
我目前用来尝试从数据网格中获取颜色并在组合框中选择正确记录的代码:
cmdColor.SelectedIndex = CmdColor.FindString(grdColors.CurrentRow.Cells["COLORCELL"].Value.ToString()`);
感谢名单
答案 0 :(得分:0)
我不太了解这个问题,但是关于找到字符串的一部分,你可以在这里找到多个解决方案。
String.Split会将您的字符串分成两部分,基于':'字符:
string dataFromTable = "1:green";
string[] dataSplitted = dataFromTable.Split(new char[] { ':' });
// now dataSplitted[0] = "1" and dataSplitted[1] = "green";
您还可以根据"使用String.IndexOf:"得到颜色的位置:
// be sure that you actually have something after your ":", else it will
// throw an OutOfRange exception
string color = dataFromTable.Substring(dataFromTable.IndexOf(":") + 1);
"最干净"根据我的喜好这样做的方法是实现一个简单的类来处理所有这些:
public class TableColor {
int id;
string color;
public static char[] separator = new char[] { ':' };
public TableColor(string data)
{
string[] dataSplitted = data.Split(separator);
id = Convert.ToInt32(dataSplitted[0]);
color = dataSplitted[1];
}
}
答案 1 :(得分:0)
//首先创建一个枚举类
namespace DemoEnum
{
public enum Color
{
RED,
YELLOW,
GREEN,
BLACK,
ORANGE,
}
}
//然后使用此命令在组合框中填充枚举值
cmdColor.DataSource = Enum.GetNames(typeof(Color));
//完成此操作后,您可以使用此命令读出组合框的值
"string" = Convert.ToString((Kleur)cmdColor.SelectedIndex);
然后字符串将是组合框的选定值。但值得注意的是,组合框索引从0开始而不是1。
希望它有效:)