1

I am trying to learn some basic data manipulation through Entity Framework. In this particular scenario, I happen to stumble upon some ambiguous lines of code I found online.

I have a Combobox, with DisplayMember property and ValueMember Property Set to particular column names of a table somewhere. My interpretation, until now, was to bind DisplayMember and ValueMember after I have set the DataSource property of Combobox as it feels more intuitive. Like this:

ComboBox1.DataSource = SalesEntity.Products.ToList();
ComboBox1.DisplayMember = "CategoryName"
ComboBox1.ValueMember = "CategoryId"

In a program that I am currently working on, I got a CastException, working with this approach. I knew it was indeed a RuntimeError, but I couldn't see my Form(the process had started, but yet not shown)! I figured out that the SelectedIndexChanged event triggers as soon as the form loads. So after a few google clicks, I came by this thread:

ComboBox.ValueMember and DisplayMember

It explained that the DataSource should be set after binding the DisplayMember and ValueMember properties with the column, in order to avoid these errors.

ComboBox1.DisplayMember = "CategoryName"
ComboBox1.ValueMember = "CategoryId"
ComboBox1.DataSource = SalesEntity.Products.ToList();

Although it worked I want to know why does this work?

My thoughts: I assume that setting the ComboBox to dataSource at first, initializes the SelectedIndex property and when the ValueMember/DisplayMember property is set, it changes the SelectedIndex property according to the default values of the type bound to these properties(ValueMember/DisplayMember). Whereas setting the DataSource, at last, avoids the reinitialization of the properties. Do correct me, if I am wrong.
Some initialization must have occurred for the event to trigger as soon as it loads. Which initialization triggers this event so fast?

Amar30657
  • 13
  • 1
  • 3
  • When you set the DisplayMember property, the data source source needs to be unwired and re-parsed. You can see it in the ComboBox implementation notes [here](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ComboBox.cs,2944) where [RefreshItems()](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ComboBox.cs,3015) is called... – Jimi Feb 05 '20 at 18:45
  • ... and in the base class [here](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ListControl.cs,87) and [here](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ListControl.cs,137). If you first set the `DisplayMember` and the `DataSource` is null, nothing happens. If you set it after... See the notes related to the [DataSource](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ListControl.cs,77) property. – Jimi Feb 05 '20 at 18:54
  • @Jimi, now that you have pointed out the link from reference, I get it. Could you please write it down, I will mark it as the answer. – Amar30657 Feb 05 '20 at 19:18

0 Answers0