问题描述
考虑以下code。这是我在,我已经创建了一个用户控件执行过程的简化。
Consider the following code. This is a simplification of a process that I have implemented within a User Control that I've created.
//MyUserControl Constructor
public MyUserControl(field, value)
{
InitializeComponents();
string cType = resolveControlType(field);
switch (cType)
{
...
case "ComboBox": AddComboBox(field, value);
...
}
}
AddComboBox(string fieldID, object value)
{
ComboBox cbo = new ComboBox();
cbo.DisplayMember = "DisplayMember";
cbo.ValueMember = "ValueMember";
//We set the DataSource to a DataTable
cbo.DataSource = DBCaller.GetListAsDataTable(fieldID);
this.Controls.Add(cbo);
cbo.SelectedValue = value; //<-- Weird stuff happening here?!
// If you don't break here, it
// doesn't look like the correct
// record is selected.
// However, add a breakpoint,
// scroll through cbo's properties
// and this assignment will work
// properly when you continue?!
}
我的问题是,当我的值赋给控制,组合框的文本从我的数据源表中显示的第一个项目。
My problem is that when I assign the value to the control, the text in the ComboBox displays the first item from my DataSource table.
但是,如果我把一个断点上的 cbo.SelectedValue =值;
行,并使用智能感知,滚动和我的组合框关联的属性,一些初始化的组合框修复此问题。有一次,我继续运行code,显示在我的组合框的形式加载与适当的值。
However, if I put a breakpoint on the cbo.SelectedValue = value;
line and, using Intellisense, scroll through the properties associated with my ComboBox, something initializes on the ComboBox that fixes this problem. Once I continue running the code, my form loads with the proper value displayed on the ComboBox.
这是怎么回事,我该如何解决这个问题?
What is going on and how can I fix this?
推荐答案
我已经找到了如何解决你的问题,但解释为什么它是如此不容易。我发现了一些有趣的东西在这里。首先,我想说,我已经发现至少有2种方式来设置整治。下面是code对于那些2种方式:
I've found how to solve your problem but to explain why it's so is not easy. I've found some thing interesting here. First, I would like to say that I've found at least 2 ways to set the things in order. Here are the code for those 2 ways:
//Solution 1
//Simply you have to add the ComboBox to the parent control first
//before assigning its DataSource
this.Controls.Add(cbo); //<---- This goes first
cbo.DataSource = DBCaller.GetListAsDataTable(fieldID); //<--- This goes after
cbo.SelectedValue = value;
//Solution 2
//This is very strange and interesting, you can also add your ComboBox to
//the parent control after assigning its DataSource (as in your code).
//But you have to ACCESS to the BindingContext property of your ComboBox
//I would like to emphasize the ACCESS, you can perform any kind of access (Read and Write).
//Here are some examples of such access:
cbo.DataSource = DBCaller.GetListAsDataTable(fieldID);
this.Controls.Add(cbo); //<--- like in your code, this is placed here after the DataSource is assigned
//here you can ACCESS the BindingContext
var whatEver = cbo.BindingContext;//READ access
if(cbo.BindingContext == null) Text = "????"; //READ access and of course it's not null
cbo.BindingContext = new BindingContext();//WRITE access
cbo.SelectedValue = value; //<---- This should be placed here after all.
我找到了第二个解决方案是很奇怪的,不容易解释,虽然第一个解决方案,可以理解的(至少当时我还没有找到第二个)。
I found the second solution is very strange and not easy to explain, although the first solution can be understandable (at least at the time I had not found the second).
这篇关于哎呀!你如何正确地更新初始化组合框的SelectedValue的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!