Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8521869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:56:55+00:00 2026-06-11T06:56:55+00:00

I have a ComboBox which is being populated by a LINQ query: var locations

  • 0

I have a ComboBox which is being populated by a LINQ query:

var locations = 
    from loc in db.LOCATIONs
        .Where(a => a.Omit == false)
select new
{
    LocationID = loc.LocationID,
    Location = loc.Location1
};

cbo = this.cboAdminLocation;
cbo.DataSource = locations;
cbo.DisplayMember = "Location";
cbo.ValueMember = "LocationID";
cbo.SelectedIndex = -1;

I also have a routine DisplayAccounts() which lists all accounts in a DataGridView, but filtered by the value selected in the combo, if selected, or displays all accounts if not. I thought this would be quite simple, and am trying to handle it with the following statements within DisplayAccounts():

if (this.cboAdminLocation.SelectedIndex > -1)
    filterAdminLocation = Convert.ToInt32(this.cboAdminLocation.SelectedValue.ToString());
else
    filterAdminLocation = 0;

and at the end of the LINQ query:

if (filterAdminLocation > 0)
{
    accountData = accountData.Where(a => a.LocationID == filterAdminLocation);
}

However, I am getting an error on the first loop round (i.e. before having a chance to select a value from the Combo) of “Input string was not in the correct format.”, and indeed the value of the combo at this stage is displayed as { LocationID = 1, Location = "Brentwood" } and the selected index is 0. If I skip over this step and test when I have selected a value it returns an integer value as expected.

Therefore, my question is why does get past the if (this.cboAdminLocation.SelectedIndex > -1) test when the selected index is supposedly 0, and more to the point why is the selected index 0 when I explicitly set it to -1 when populating the control?

I appreciate this is probably very simple, but I’m fairly new to C# so you’ll have to excuse my stupidity!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:56:57+00:00Added an answer on June 11, 2026 at 6:56 am

    Therefore, my question is why does get past the if (this.cboAdminLocation.SelectedIndex > -1) test when the selected index is supposedly 0, and more to the point why is the selected index 0 when I explicitly set it to -1 when populating the control?

    This is because you first populate the comboBox, then set the selected index to -1. Between this two steps, the selected index is – for a short time – 0.

    In order to correct that, you could call your DisplayAccounts() routine only after you’ve run cbo.SelectedIndex = -1;. I guess you call DisplayAccounts() in the comboBox’ SelectedIndexChanged event. If so, attach the event to the comboBox after it (and be sure to remove it from the Designer.cs file if you’ve added the event in the Designer window):

    //...
    cbo = this.cboAdminLocation;
    cbo.DataSource = locations;
    cbo.DisplayMember = "Location";
    cbo.ValueMember = "LocationID";
    cbo.SelectedIndex = -1;
    cbo.SelectedIndexChanged += new EventHandler(cbo_SelectedIndexChanged);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a combobox which is displaying date from database...in my database the date
I have a combobox which is populated by the Keys enumeration (winforms). The problem
I have a combobox from which i need to programmatically disable items depending on
I have a datagridview and a combobox which get populated randomly with data. However,
I have a combobox in winforms, which, depending on what you select, determines what's
I have a combobox which binds data from the database. It gets the data
I have a simple UserControl containing a ComboBox which is empty at first. The
I have one combobox on the window form and I have one method which
I have a JSON data store used for a combobox selection which is working
I have the following collection which i would like to bind to a combobox:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.