C# Designer overwrites ComboBox DataSource
I am trying to set the DataSource of a ComboBox programmatically in InitializeComponent.
this.comboBox1.DataSource = (IEnumerable<> from a 开发者_如何转开发Linq command);
I save the file, everything works fine. However, as soon as I open the Designer, it overwrites my code. What should I set in the Properties page to prevent Designer replaces my code with:
this.comboBox1.Items.AddRange( new object[] {
"value1",
"value2",
...
}
where the hardcoded list is the result of the Linq command. Since the DataSource is set in an early line, the Designer throw exception complaining "DataSource cannot be changed after it's set".
What should I specify on the Properties to prevent this from happening? Thanks.
You should not be editing InitializeComponent manually. Instead, you should put that code in the constructor (or Load event) of your form/control.
精彩评论