Can't read SelectedValue from Listbox

  • Thread starter Thread starter Mantova
  • Start date Start date
M

Mantova

Maybe it's simple, but I can't get it to work.
I'm originally a webdeveloper, and just started to work on a C# windows
application.
I want a list, something similar to a <select><option> object, where the
displayed value is different from the real value.
I populate the list like this, where I assume that SelectedValue is
something like the <option value="ID">

while (sql.Read())
{
currentItem=this.List.Items.Add(sql["name"]);
List.SelectedIndex=currentItem;
List.SelectedValue=sql["id"];
}

Now when I select an item in the list and do something like
textbox.Text=List.SelectedValue.ToString();
it gives me an error.
Debugging gives me a <undisplayable value> for the SelectedValue.
What am I doing wrong here?
 
Here is an examples drop down as generate by Visual Studio .NET 2003 ...

<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="ddlA" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 184px" runat="server" AutoPostBack="True" Width="217">
<asp:ListItem Value="Computer Industry" Selected="True">Computer
Industry</asp:ListItem>
<asp:ListItem Value="msdn.microsoft.com/msdnmag/">MSDN
Magazine</asp:ListItem>
<asp:ListItem Value="www.nwfusion.com/">NetworkWorld</asp:ListItem>
<asp:ListItem Value="www.zdnet.com/pcmag/">PC Magazine</asp:ListItem>
<asp:ListItem Value="www.web-developer.com/">Web
Developer</asp:ListItem>
<asp:ListItem Value="www.webtechniques.com/">Web
Techniques</asp:ListItem>
<asp:ListItem Value="www.win2000mag.com/">Windows 2000
Magazine</asp:ListItem>
<asp:ListItem Value="www.ntsystems.com/">Windows NT
Systems</asp:ListItem>
<asp:ListItem Value="www.wired.com/">Wired Magazine</asp:ListItem>
</asp:dropdownlist>
</form>
 
Hi Mantova,

I'm sorry, but I don't know why your code works as the documents seem to indicate you can set the value manually without a ValueMember. I also could not set the Value. It may be that a DataSource is required.

An alternate solution is to use an ArrayList as a source (also see the USState example in the documents), or a DataSet

ArrayList dataList = new ArrayList();

while(sql.Read())
{
dataList.Add(new MyObject(sql["name"], sql["id"]));
}

List.DisplayMember = "MyDisplay";
List.ValueMember = "MyValue";
List.DataSource = dataList;

....

public class MyObject
{
private string disp;
private int val;

public string MyDisplay
{
get{return disp;}
}
public int MyValue
{
get{return val;}
}

public MyObject(string display, int id}
{
disp = display;
val = id;
}
}
 
Thom Little said:
Here is an examples drop down as generate by Visual Studio .NET 2003 ...

<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="ddlA" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 184px" runat="server" AutoPostBack="True" Width="217">
<asp:ListItem Value="Computer Industry" Selected="True">Computer
Industry</asp:ListItem>
<asp:ListItem Value="msdn.microsoft.com/msdnmag/">MSDN
Magazine</asp:ListItem>
<asp:ListItem Value="www.nwfusion.com/">NetworkWorld</asp:ListItem>
<asp:ListItem Value="www.zdnet.com/pcmag/">PC Magazine</asp:ListItem>
<asp:ListItem Value="www.web-developer.com/">Web
Developer</asp:ListItem>
<asp:ListItem Value="www.webtechniques.com/">Web
Techniques</asp:ListItem>
<asp:ListItem Value="www.win2000mag.com/">Windows 2000
Magazine</asp:ListItem>
<asp:ListItem Value="www.ntsystems.com/">Windows NT
Systems</asp:ListItem>
<asp:ListItem Value="www.wired.com/">Wired Magazine</asp:ListItem>
</asp:dropdownlist>
</form>

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

Mantova said:
Maybe it's simple, but I can't get it to work.
I'm originally a webdeveloper, and just started to work on a C# windows
application.
I want a list, something similar to a <select><option> object, where the
displayed value is different from the real value.
I populate the list like this, where I assume that SelectedValue is
something like the <option value="ID">

while (sql.Read())
{
currentItem=this.List.Items.Add(sql["name"]);
List.SelectedIndex=currentItem;
List.SelectedValue=sql["id"];
}

Now when I select an item in the list and do something like
textbox.Text=List.SelectedValue.ToString();
it gives me an error.
Debugging gives me a <undisplayable value> for the SelectedValue.
What am I doing wrong here?

It's a Windows application, not web application..but thanx anyway
 
Morten Wennevik said:
Hi Mantova,

I'm sorry, but I don't know why your code works as the documents seem to
indicate you can set the value manually without a ValueMember. I also could
not set the Value. It may be that a DataSource is required.
An alternate solution is to use an ArrayList as a source (also see the
USState example in the documents), or a DataSet
ArrayList dataList = new ArrayList();

while(sql.Read())
{
dataList.Add(new MyObject(sql["name"], sql["id"]));
}

List.DisplayMember = "MyDisplay";
List.ValueMember = "MyValue";
List.DataSource = dataList;

...

public class MyObject
{
private string disp;
private int val;

public string MyDisplay
{
get{return disp;}
}
public int MyValue
{
get{return val;}
}

public MyObject(string display, int id}
{
disp = display;
val = id;
}
}

hmmm...maybe I'll try that.
But I hoped there would be a more elegant solution, like in <select><option>

Thanx for yer help

Greets,
DIck
 
Back
Top