469,600 Members | 2,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

DropDownList and ListItem

Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how do
I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer
Nov 16 '05 #1
10 17263
Fabio,

The DropDownList doesn't exist in Windows Forms. I assume you mean the
ComboBox control. In this case, you would use the Items property, which is
the indexer (I believe) for the class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how
do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer

Nov 16 '05 #2
Fabio,

I should correct myself, Items is not the indexer, but you would still
use that property (it exposes a collection) to add items to the list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how
do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer

Nov 16 '05 #3
Nicholas, firstly I really appreciate your attention.

I really should mean ComboBox (windows form). But the problem is I want to
get the ID and not the indexed value... If I realised what you said I would
get the value "1" for the second item instead of "6" as I wanted.

Fabio Negri Cicotti
Software Engineer

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OC**************@tk2msftngp13.phx.gbl...
Fabio,

I should correct myself, Items is not the indexer, but you would still
use that property (it exposes a collection) to add items to the list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how
do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer


Nov 16 '05 #4
Fabio,

You can't do that with a ComboBox, if you want to get a value other than
the display value, then you would have to use data binding. It's simple
enough to do, just wire up a data table with two columns and then set that
as the data source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:O4*************@TK2MSFTNGP12.phx.gbl...
Nicholas, firstly I really appreciate your attention.

I really should mean ComboBox (windows form). But the problem is I want
to get the ID and not the indexed value... If I realised what you said I
would get the value "1" for the second item instead of "6" as I wanted.

Fabio Negri Cicotti
Software Engineer

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:OC**************@tk2msftngp13.phx.gbl...
Fabio,

I should correct myself, Items is not the indexer, but you would still
use that property (it exposes a collection) to add items to the list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how
do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer



Nov 16 '05 #5
Nicholas - a jargon question - what do you mean by 'wire up'?

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
Fabio,

You can't do that with a ComboBox, if you want to get a value other
than the display value, then you would have to use data binding. It's
simple enough to do, just wire up a data table with two columns and then
set that as the data source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:O4*************@TK2MSFTNGP12.phx.gbl...
Nicholas, firstly I really appreciate your attention.

I really should mean ComboBox (windows form). But the problem is I want
to get the ID and not the indexed value... If I realised what you said I
would get the value "1" for the second item instead of "6" as I wanted.

Fabio Negri Cicotti
Software Engineer

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:OC**************@tk2msftngp13.phx.gbl...
Fabio,

I should correct myself, Items is not the indexer, but you would
still use that property (it exposes a collection) to add items to the
list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code),
how do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer



Nov 16 '05 #6
Peter,

I just mean create. You can take a data set, add a table, add columns,
and then populate them all in code. It's almost like having a home theatre.
The parts are all there, you just have to wire it up (forget about the fact
that they are all separate components). With this, the whole structure for
the data set is pre-existing, you just have to set it up.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Peter N Roth" <re***********@mycompany.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
Nicholas - a jargon question - what do you mean by 'wire up'?

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Fabio,

You can't do that with a ComboBox, if you want to get a value other
than the display value, then you would have to use data binding. It's
simple enough to do, just wire up a data table with two columns and then
set that as the data source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:O4*************@TK2MSFTNGP12.phx.gbl...
Nicholas, firstly I really appreciate your attention.

I really should mean ComboBox (windows form). But the problem is I want
to get the ID and not the indexed value... If I realised what you said I
would get the value "1" for the second item instead of "6" as I wanted.

Fabio Negri Cicotti
Software Engineer

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:OC**************@tk2msftngp13.phx.gbl...
Fabio,

I should correct myself, Items is not the indexer, but you would
still use that property (it exposes a collection) to add items to the
list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
> Sorry for my silly question...
>
> in the same way we can define in a DropDownList the "ValueMember" and
> "Display Member" when we bind a DatasSource, how could I do it using a
> hard-code fixed datasource (without neither access the DB or create a
> DataTable)?
>
> To be as clear as possible, if I want to fill a DropDownList with the
> following data (not comming from database, but from my source-code),
> how do I do?
>
> ID - Desciption
> 3 - Desc 3
> 6 - Desc 6.1
> 7 - Desc 7
>
> With ASP.NET I use ListItem but it seems don't work with windows
> forms.
>
>
> Kind regards,
>
> Fabio Negri Cicotti
> Software Engineer
>



Nov 16 '05 #7
The weird thing which I don't accept at all is the fact I could do it even
in VB6 and now I can't anymore!

I agree tables are easy to use but I don't like the time and code (I mean
number of lines) wasted to make it!
Thanks anyway.

Fabio Negri Cicotti
Software Engineer

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
Fabio,

You can't do that with a ComboBox, if you want to get a value other
than the display value, then you would have to use data binding. It's
simple enough to do, just wire up a data table with two columns and then
set that as the data source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:O4*************@TK2MSFTNGP12.phx.gbl...
Nicholas, firstly I really appreciate your attention.

I really should mean ComboBox (windows form). But the problem is I want
to get the ID and not the indexed value... If I realised what you said I
would get the value "1" for the second item instead of "6" as I wanted.

Fabio Negri Cicotti
Software Engineer

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:OC**************@tk2msftngp13.phx.gbl...
Fabio,

I should correct myself, Items is not the indexer, but you would
still use that property (it exposes a collection) to add items to the
list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code),
how do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer



Nov 16 '05 #8
After the Nicholas' answers (thanks again) I decided to create a function to
make that task more easy. Now, I pass an ArrayList with the values to the
function, as shown below. If anyone has another idea to optimize this
function, please let us know. Thanks all.
ArrayList ValuesArray = new ArrayList();
ValuesArray.Add(new string[] {"3", "Desc 3"});
ValuesArray.Add(new string[] {"6", "Desc 6.1"});
ValuesArray.Add(new string[] {"7", "Desc 7"});
FillCombo(cboSomeThing, ValuesArray);
private void FillCombo(ComboBox comboBox, ArrayList ListItem) {
DataTable rsTemp = new DataTable("Temp");
rsTemp.Columns.Add("ValueMember", typeof(Int32));
rsTemp.Columns.Add("DisplayMember", typeof(String));

for (int Count = 0; Count < ListItem.Count; Count++) {
DataRow drwTemp = rsTemp.NewRow();
drwTemp["ValueMember"] =
Convert.ToInt32(((string[])ListItem[Count])[0]);
drwTemp["DisplayMember"] = ((string[])ListItem[Count])[1].ToString();
rsTemp.Rows.Add(drwTemp);
}

comboBox.ValueMember = "ValueMember";
comboBox.DisplayMember = "DisplayMember";
comboBox.DataSource = rsTemp;
}

--
Fabio Negri Cicotti
Software Engineer

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how
do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer

Nov 16 '05 #9
gotcha. thx.

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OO**************@TK2MSFTNGP09.phx.gbl...
Peter,

I just mean create. You can take a data set, add a table, add columns,
and then populate them all in code. It's almost like having a home
theatre. The parts are all there, you just have to wire it up (forget
about the fact that they are all separate components). With this, the
whole structure for the data set is pre-existing, you just have to set it
up.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Nov 16 '05 #10
Fabio,

The only recommendation I would make would be to have a function which
creates the data table and passes it back, empty, and then populate that
directly, either before or after you bind it to the table. This way, if you
need to add values to the list, you can just add them to the data set that
you are holding (assuming you hold onto it and need it for this purpose).
Other than that, it looks fine.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:uu***************@TK2MSFTNGP09.phx.gbl...
After the Nicholas' answers (thanks again) I decided to create a function
to make that task more easy. Now, I pass an ArrayList with the values to
the function, as shown below. If anyone has another idea to optimize this
function, please let us know. Thanks all.
ArrayList ValuesArray = new ArrayList();
ValuesArray.Add(new string[] {"3", "Desc 3"});
ValuesArray.Add(new string[] {"6", "Desc 6.1"});
ValuesArray.Add(new string[] {"7", "Desc 7"});
FillCombo(cboSomeThing, ValuesArray);
private void FillCombo(ComboBox comboBox, ArrayList ListItem) {
DataTable rsTemp = new DataTable("Temp");
rsTemp.Columns.Add("ValueMember", typeof(Int32));
rsTemp.Columns.Add("DisplayMember", typeof(String));

for (int Count = 0; Count < ListItem.Count; Count++) {
DataRow drwTemp = rsTemp.NewRow();
drwTemp["ValueMember"] =
Convert.ToInt32(((string[])ListItem[Count])[0]);
drwTemp["DisplayMember"] = ((string[])ListItem[Count])[1].ToString();
rsTemp.Rows.Add(drwTemp);
}

comboBox.ValueMember = "ValueMember";
comboBox.DisplayMember = "DisplayMember";
comboBox.DataSource = rsTemp;
}

--
Fabio Negri Cicotti
Software Engineer

"Fabio Negri Cicotti [MCP]" <ci*****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
Sorry for my silly question...

in the same way we can define in a DropDownList the "ValueMember" and
"Display Member" when we bind a DatasSource, how could I do it using a
hard-code fixed datasource (without neither access the DB or create a
DataTable)?

To be as clear as possible, if I want to fill a DropDownList with the
following data (not comming from database, but from my source-code), how
do I do?

ID - Desciption
3 - Desc 3
6 - Desc 6.1
7 - Desc 7

With ASP.NET I use ListItem but it seems don't work with windows forms.
Kind regards,

Fabio Negri Cicotti
Software Engineer


Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by huzz | last post: by
reply views Thread by Daniel Doyle | last post: by
3 posts views Thread by Jeff | last post: by
2 posts views Thread by =?Utf-8?B?VmljdG9yaW91czE=?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.