DropDownList and ListItem

  • Thread starter Thread starter Fabio Negri Cicotti [MCP]
  • Start date Start date
F

Fabio Negri Cicotti [MCP]

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
 
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.
 
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, 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 said:
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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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
 
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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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 said:
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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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
 
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 said:
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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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 said:
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]
- (e-mail address removed)

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
 
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]
- (e-mail address removed)


Peter N Roth said:
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 said:
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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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

in message 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]
- (e-mail address removed)

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
 
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 said:
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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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 said:
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]
- (e-mail address removed)

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
 
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,

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]
- (e-mail address removed)

Fabio Negri Cicotti said:
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 said:
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
 
Back
Top