Obtaining a Value from a databound listbox using NameValueListBase - Solved

Obtaining a Value from a databound listbox using NameValueListBase - Solved

Old forum URL: forums.lhotka.net/forums/t/2819.aspx


CSLABound posted on Thursday, May 03, 2007

I'm sure this is a newbie question.... but I've pulled every book off of Barnes and Nobles and Books a Million as well as looking through online forums, and can't find an answer.

I have a NameValueListBase (a list of Admin Settings for the app) databound to a windows form UI listbox.  I want to grab the integer representation of an item in the NameValueListBase, within a button click method.

So there is a public class AdminSettingsList: NameValueListBase<int, string>

there is an adminSettingsListBindingSource

and there is an AdminSettingsListBox.

When a user clicks on the edit button, I need to grab the int of the selected Admin Setting, to populate a grid control with another object that corresponds to that Admin setting.  How do I get the integer?

Thanks

CSLABound replied on Thursday, May 03, 2007

Sometimes I think just posting to this forum pulls the answer out of my head.  Let me apologize for figuring it out 2 seconds after posting this.  But the answer I came up with is the following:

int integerSelected = int.Parse(this.AdminSettingsListBox.SelectedValue.ToStrng());

 

tetranz replied on Friday, May 04, 2007

CSLABound:
But the answer I came up with is the following:
int integerSelected = int.Parse(this.AdminSettingsListBox.SelectedValue.ToStrng());

That probably works but its making the data jump through lots of unnecessary hoops.

AdminSettingsListBox.SelectedItem gives you a reference to the selected object in the collection which will be of type AdminSettingsList.NameValuePair.  That has the strongly typed properties Key and Value. I think a cleaner answer is:

int integerSelected = ((AdminSettingsList.NameValuePair) AdminSettingsListBox.SelectedItem).Key

Cheers
Ross

ajj3085 replied on Friday, May 04, 2007

Well, as long as we're throwing out alternate methods to acomplish this:

int integerSelected = (int)AdminSettingsListBox.SelectedValue;

Would be the shortest path to the value, assuming the Value property of the combobox is set to the interger column.

Copyright (c) Marimer LLC