• 0

[C#] ComboBox Specified Cast Not Valid!


Question

This is the code:

cmbBalSheetCat.Items.Clear();
cmbBalSheetCat.DataSource = dtResults;
cmbBalSheetCat.ValueMember = "BalanceSheetCategoryID";
cmbBalSheetCat.DisplayMember = "BalanceSheetCategoryName";
cmbBalSheetCat.SelectedIndex = -1;

It errors on setting the .ValueMember saying the 'Specified cast is not valid'.

dtResults is populated like so:

dtResults = new DataTable();
dtResults.Columns.Add("BalanceSheetCategoryIDddtResults.Columns.Add("BalanceSheetCategoryNameddtResults.Columns.Add("BalanceSheetCategoryOrderforeach( BalSheetCatData objBalSheetCatData in this.Values )
{
	DataRow dtRow = dtBalSheetCats.NewRow();
	dtRow["BalanceSheetCategoryID"] = objBalSheetCatData.BalanceSheetCategoryID;
	dtRow["BalanceSheetCategoryName"] = objBalSheetCatData.BalanceSheetCategoryName;
	dtRow["BalanceSheetCategoryOrder"] = objBalSheetCatData.BalanceSheetCategoryOrder;
	dtResults.Rows.Add( dtRow );
}

I dont understand why im getting this error. I have similar code for a combo on a different form that works fine!!

Do i need to specify column types or something? If so, how comes it works fine without it on other forms and not this one?

Any ideas?

Link to comment
https://www.neowin.net/forum/topic/416380-c-combobox-specified-cast-not-valid/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

__________________________________________________________________

Ah ok... just found its not the .ValueMember line that it is erroring on. Its because the combo has an associated _SelectedIndexChanged() method and its erroring inside there.

To fix the problem, ive used a boolean value to store whether the combo data has been loaded and the _SelectedIndexChanged() only executes code when that boolean is true.

But this seems somewhat feeble... is there a better way of doing this? A setting ive missed?

  • 0

Just a quick note to give u a helping hand, Databinding an Array of objects or values (MyObject[]) isn't the same as binding an ArrayList of MyObject for example. The former been non enumerable by default and wont store the combobox wont store ValueMember.

Ie.

MyObject[] objMyObjects = null;

... initialize myobjects array...

Combobox1.DisplayMember = "Description";

Combobox1.ValueMember = "ID";

Combobox1.DataSource = objMyObjects;

this will cause the Combobox1 to not hold the selected objects value as it isnt Enumerable. A nice way around this would be too simply to do following...

MyObject[] objMyObjects = null;

... initialize myobjects array...

Combobox1.DisplayMember = "Description";

Combobox1.ValueMember = "ID";

Combobox1.DataSource = new ArrayList(objMyObjects);

This will enable enumeration for ur combobox, and as such it will hold the values correctly.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • So, do nothing basically, as long as you deploy Windows Updates? Pretty much yeah. Some Linux distros distribute update the secureboot certs as well, assuming you do use SecureBoot.
    • On one hand, YouTube videos are filled with so much fillers and the Youtubers intentionally speak slowly to increase video time and "engagement" metrics. On the other, Google's asking you to not stay on their site for longer. That's a win-win for viewers. So, I think it'll be axed or de-emphasised in the near future.
    • They'll just repurpose that for their AI trainings. Its never enough for LLMs.
    • Ubuntu gets second-ever snapshot release for Questing Quokka by Paul Hill Canonical has announced the release of Ubuntu 25.10 Questing Quokka Snapshot 2, a monthly development build that gives testers and developers a base from which to work on software for the upcoming release. Snapshot 1 was released at the end of May and Snapshot 3 is scheduled for July 31. Notably, the release date of Snapshot 2 and 3 have moved since last month. The Snapshot 2 update is available for various Ubuntu spins, such as Kubuntu and Lubuntu. To download, head to Ubuntu CD Image and go to the link for the version you want, such as ubuntu/. Once you’ve picked, go to releases/ > 25.10/ > snapshot-2/ and download the appropriate image for your computer - most people will want ‘64-bit PC (AMD64) desktop image’. The announcement mentions that these snapshot builds are not production ready, so you should not be installing them on a machine you use to do your work and daily computing. Canonical said that these builds should be seen as “throwaway artifacts”, whatever that means. If you’re an Ubuntu developer, you should submit your changes in the Ubuntu archive by July 28 to see it in the third snapshot. If you make any changes, Canonical asks you to update the Release Notes with the updates that you have worked on, so everyone knows what changed. Speaking of release notes, Canonical has been updating them incrementally. So far, we know that GNOME 48 is being used alongside the Linux 6.14 kernel. The use of GNOME 48 means that Ubuntu 25.10 only supports Wayland sessions as X.org has finally been dropped. Wayland has been used for a while on Ubuntu, so most people shouldn't have any issues as a result of the switchover. If you want to try out Ubuntu 25.10 Snapshot 2, you can find the download links over on the Ubuntu website. Just remember, these are not intended to be used on production machines!
  • Recent Achievements

    • Week One Done
      Marites earned a badge
      Week One Done
    • One Year In
      runge100 earned a badge
      One Year In
    • One Month Later
      runge100 earned a badge
      One Month Later
    • One Month Later
      jfam earned a badge
      One Month Later
    • First Post
      TheRingmaster earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      559
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      168
    4. 4
      Michael Scrip
      125
    5. 5
      Xenon
      118
  • Tell a friend

    Love Neowin? Tell a friend!