Re: ListBoxes



David,

What's wrong with using the SelectedValue property? It will return the
value that the list is bound to (and I'm surprized that the listbox worked
in .NET 1.1 with the Items collection when it was data bound).

You just have to do this:

// Get the number.
string number = StaffList.SelectedValue.ToString();

If you want an int, you can do this:

// Get the number.
int number = (int) StaffList.SelectedValue;

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx



"David Gouge" <dave@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:u722uCr6FHA.1188@xxxxxxxxxxxxxxxxxxxxxxx
> Hi All,
>
> Hope someone can point me in the right direction.
>
> Doing a very simple app in C# .net 2.0 where i have a listbox bound to a
> datatable (StaffTable) with the DisplayMember being the name field and the
> ValueMember being the number field. I have the following code in the
> form's constructor:
>
> StaffList.DataSource = StaffTable;
> StaffList.DisplayMember = "name";
> StaffList.ValueMember = "number";
>
> What i need to do is grab the data from the ValueMember field for the
> selected ListBox item. So far i have only found the following way to do
> this:
>
> DataRowView aDrv = (DataRowView)StaffList.SelectedItem;
> string number = aDrv.Row[2].ToString().Trim();
>
> ...which seems a bit of a long way round.
>
> In .net 1.1, all i had to do was:
>
> string number = StaffList.Items[StaffList.SelectedIndex].Value
>
> ...but this does not build in 2.0.
>
> So, is the way i'm doing it the only way in 2.0 or is there are more
> elegant way as in my 1.1 code?
>
> Thanks in advance.


.



Relevant Pages

  • Re: windows forms listbox item value
    ... > Windows Forms listbox have a SelectedValue property that is used to ... How do I retrieve ...
    (microsoft.public.dotnet.general)
  • windows forms listbox item value
    ... Windows Forms listbox have a SelectedValue property that is used to retrieve ...
    (microsoft.public.dotnet.general)
  • RE: How do I disable vertical scroll bar in a listbox?
    ... When there're more items in a ListBox that can show in the ListBox, ... vertical scroll bar, if present, within a ListBox. ... int WM_PRINTCLIENT = 0x0318; ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • RE: How do I disable vertical scroll bar in a listbox?
    ... When there're more items in a ListBox that can show in the ListBox, ... vertical scroll bar, if present, within a ListBox. ... int WM_PRINTCLIENT = 0x0318; ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: ListBox and UseTabStops = true: How to set the TabStops positions ?
    ... Create a new C# Windows application. ... Add a command button and listbox to the default form. ... private static extern int SendMessage (int hWnd, int wMsg, int wParam, ref ...
    (microsoft.public.dotnet.framework.windowsforms.controls)