DataBound ComboBox Problem

Problem:
When you want to use a data bound ComboBox in .NET (C#, VB, etc), you should set a few properties such as DataSource, DisplayMember and ValueMember.
It works fine unless you want to set SelectedValue for the first time. It won’t be set.

Solution:
I found that BindingContext property must be used before initializing SelectedValue:

ComboBox1.BindingContext = new BindingContext();
ComboBox1.DataSource = MyDataSet.DefaultView
ComboBox1.DisplayMember = “Title”;
ComboBox1.ValueMember = “Code”;
ComboBox1.SelectedValue = 1;

2 Responses to “DataBound ComboBox Problem”

  1. Mohammad Says:

    Good starting my friend! Good luck with your writing :-)

Leave a Reply