473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ComboBox Setting both DataSource and SelectedIndex

This problem should be simple.

I am using Visual Studio 2003, Version 1.1

I am writing a desktop application in which I am binding a DataTable
to the DataSource of a ComboBox.
// I have 50 rows (concerning states) in dataTable.Rows;

ComboBox myComboBox = new ComboBox();
myComboBox.DataSource = dataTable;
myComboBox.DisplayMember = "state_name";
myComboBox.ValueMember = "state_id";
myComboBox.SelectedIndex = 3;

I get an exception stating that '3' is out of range, because
myComboBox.Items.Count = 0, not 50.

How can I set SelectedIndex when using a DataSource?

A very messy workaround is to add the items in a loop, but I would
like to use the power of DataSource.

P.S. In ASP.NET they have a DataBind() method which is absent for a
desktop application.

Thanks for your help.
Feb 29 '08 #1
2 15530
Try adding a dummy binding context to it before setting the DataSource.
I think that will trick the ListControl into populating items when the
DataSource is set.

ComboBox myComboBox = new ComboBox();
myComboBox.BindingContext = new BindingContext();
....
sa**********@gmail.com wrote:
This problem should be simple.

I am using Visual Studio 2003, Version 1.1

I am writing a desktop application in which I am binding a DataTable
to the DataSource of a ComboBox.
// I have 50 rows (concerning states) in dataTable.Rows;

ComboBox myComboBox = new ComboBox();
myComboBox.DataSource = dataTable;
myComboBox.DisplayMember = "state_name";
myComboBox.ValueMember = "state_id";
myComboBox.SelectedIndex = 3;

I get an exception stating that '3' is out of range, because
myComboBox.Items.Count = 0, not 50.

How can I set SelectedIndex when using a DataSource?

A very messy workaround is to add the items in a loop, but I would
like to use the power of DataSource.

P.S. In ASP.NET they have a DataBind() method which is absent for a
desktop application.

Thanks for your help.
Feb 29 '08 #2
Mufaka:

Thanks so very much!

That works!

--Sam
Feb 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
3
by: ScottO | last post by:
I would like the user to have to select something in a System.Windows.Forms.ComboBox. private void MyForm_Load(object sender, System.EventArgs e) { ... comboBox.DataSource = data;...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
7
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
0
by: Alex K. | last post by:
Hi all I noticed that if I link a combobox to data table (setting DataSource property), then it is very hard to clear selection by setting SelectedIndex = -1. I have to set SelectedIndex = -1...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
5
by: Peter M. | last post by:
I'm struggling with combobox databinding with something I consider a bug... I'm binding my combobox to an array of structs. The struct exposes two public properties, ID and Name, to be used as...
5
by: Crazy Cat | last post by:
Hi all, I have combobox that is bound to a custom object collection thusly Dim collection As List(Of StructureType) = StructureType.FindStructureTypes(SharedObjects.StructureTypes,...
1
by: Andrus | last post by:
I need to enter null value from combobox to business object property. My combobox datasource does not contain ValueMember with null value. So I tried to create combobox which stores null to bound...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.