I generated a very simple code snippet:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Title", typeof(string));
dt.Rows.Add(1, "One");
dt.Rows.Add(2, "Two");
cmb = new ComboBox();
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.DisplayMember = "Title";
cmb.ValueMember = "ID";
cmb.DataSource = dt;
this.Controls.Add(cmb);
cmb.SelectedValue = 2;
}
}
When I set the value cmb.SelectedValue
, SelectedValue
is null
.
I know that if I move this code to the Form1_Load
handler, it will work as expected, but I need it in Form's Constructor.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…