array to listbox

M

Mike

I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how can I get
all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.
 
A

Alex Meleta

Hi Mike,

Use foreach(ListItem listItem in CarList.Items) { listItem.Selected = true;
} instead.

CarList.Selected<someting> works with one item only.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
M> highlight (select) all the items in the listbox were the value equals
M> (1,2,3,4,5,6,7). I'm able to only get the last value selected, how
M> can I get
M> all of them selected?
M> The listbox selectionMode= mulitple
M> for (int i = 0; i < Id.Length; i++)
M> {
M> CarList.SelectedValue = Id;
M> }
M> this is only getting me the last value being passed in from the array
M> selected. I want all of the values passed in selected.
M>
 
M

Mike

if the values are coming in as 1,2,3,4,5,6,7, how would the foreach work?
I've tried that and it didn't work due to the comma's, so I had to split it
and I'm getting the last one passed (7) selected
 
M

Mike

I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
Eliyahu Goldin said:
CarList.Items[Id].Selected = true;


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how can I
get all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.

 
E

Eliyahu Goldin

What's the type of Id? If it is integer, the code should compile. If it
is string, convert it first to integer as
System.Convert.ToInt32(Id)

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
Eliyahu Goldin said:
CarList.Items[Id].Selected = true;


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how can I
get all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.


 
A

Alex Meleta

Hi Mike,

Ou, yes, it's for select all items.

However, the common idea was to use CarList.Items[<index>].Selected to select
particular item and CarList.Items[<index>].Text to determinate the value
to match.

E.g. you can chance the code to something like this:

for (int i = 0; i < CarList.Items.Count; i++)
{
if (Id.Contains(CarList.Items.Value)) // I dunno the type of Id array
(probably string), change it to you needs
{
CarList.Items.Selected = true;
}
}

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> if the values are coming in as 1,2,3,4,5,6,7, how would the foreach
M> work? I've tried that and it didn't work due to the comma's, so I had
M> to split it and I'm getting the last one passed (7) selected
M>
M> M>
Hi Mike,

Use foreach(ListItem listItem in CarList.Items) { listItem.Selected =
true; } instead.

CarList.Selected<someting> works with one item only.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
M> I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want
to
M> highlight (select) all the items in the listbox were the value
equals
M> (1,2,3,4,5,6,7). I'm able to only get the last value selected, how
M> can I get
M> all of them selected?
M> The listbox selectionMode= mulitple
M> for (int i = 0; i < Id.Length; i++)
M> {
M> CarList.SelectedValue = Id;
M> }
M> this is only getting me the last value being passed in from the
array
M> selected. I want all of the values passed in selected.
M>
 
P

Patrice

My crystal ball tells me this is a type problem (for example split might
well create a string array so you would need to convert Id to an
integer).

Please always post the error message when you have an error ???!!!!

--
Patrice

Mike said:
I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
Eliyahu Goldin said:
CarList.Items[Id].Selected = true;


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how can I
get all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.


 
A

Alex Meleta

Hi Mike,

See msdn. If Id is type of int it should be compiled.

E.g. this will be compiled:
List<int> Id = new List<int>();
ListBox1.Items[Id].Selected = true;

and this is not:
List<string> Id = new List<string>();
ListBox1.Items[Id].Selected = true;

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> I can't even compile with this:
M>
M> CarList.Items[Id].Selected = true;
M>
M> I get error messages
M> "Eliyahu Goldin said:
CarList.Items[Id].Selected = true;

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value
equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how
can I
get all of them selected?
The listbox selectionMode= mulitple
for (int i = 0; i < Id.Length; i++)
{
CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the
array
selected. I want all of the values passed in selected.
 
M

Mike

its an interger:

I have this:

for (int i = 0; i < Id.Length; i++)
{
CarList.Items[Convert.ToInt32(ID)].Selected = true;
}

and I get this when I run it now:
Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index

and its referring to this line:
CarList.Items[Convert.ToInt32(ID)].Selected = true;

the id value is coming through as (1,2,3,4,5,6,7)
then I split it to remove the comma's

Eliyahu Goldin said:
What's the type of Id? If it is integer, the code should compile. If it
is string, convert it first to integer as
System.Convert.ToInt32(Id)

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
Eliyahu Goldin said:
CarList.Items[Id].Selected = true;


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how can
I get all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.


 
M

Mike

this is driving me nuts,
I tried the code snippet and nothing is highlighted. Its not even executing
the (IF) logic of the code.


for (int i = 0; i < CarList.Items.Count; i++)
{
if (Id.Contains(CarList.Items.Value)) // I dunno the type of Id array
(probably string), change it to you needs {
CarList.Items.Selected = true;
}
}





Alex Meleta said:
Hi Mike,

Ou, yes, it's for select all items.
However, the common idea was to use CarList.Items[<index>].Selected to
select particular item and CarList.Items[<index>].Text to determinate the
value to match.

E.g. you can chance the code to something like this:
for (int i = 0; i < CarList.Items.Count; i++)
{
if (Id.Contains(CarList.Items.Value)) // I dunno the type of Id array
(probably string), change it to you needs {
CarList.Items.Selected = true;
}
}

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> if the values are coming in as 1,2,3,4,5,6,7, how would the foreach
M> work? I've tried that and it didn't work due to the comma's, so I had
M> to split it and I'm getting the last one passed (7) selected
M> M>
Hi Mike,

Use foreach(ListItem listItem in CarList.Items) { listItem.Selected =
true; } instead.

CarList.Selected<someting> works with one item only.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
M> I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want
to
M> highlight (select) all the items in the listbox were the value
equals
M> (1,2,3,4,5,6,7). I'm able to only get the last value selected, how
M> can I get
M> all of them selected?
M> The listbox selectionMode= mulitple
M> for (int i = 0; i < Id.Length; i++)
M> {
M> CarList.SelectedValue = Id;
M> }
M> this is only getting me the last value being passed in from the
array
M> selected. I want all of the values passed in selected.
M>

 
P

Patrice

Ah great ! This error message means that the index is out of the allowed
bounds. For example is 1 the first element ? It should be 0.

Depending what you are doing you may want to use 0,1,2,3,4,5,6 instead of
1,2,3,4,5,6,7 if you are using the order of those elements in the
collection.

--
Patrice

Mike said:
its an interger:

I have this:

for (int i = 0; i < Id.Length; i++)
{
CarList.Items[Convert.ToInt32(ID)].Selected = true;
}

and I get this when I run it now:
Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index

and its referring to this line:
CarList.Items[Convert.ToInt32(ID)].Selected = true;

the id value is coming through as (1,2,3,4,5,6,7)
then I split it to remove the comma's

Eliyahu Goldin said:
What's the type of Id? If it is integer, the code should compile. If
it is string, convert it first to integer as
System.Convert.ToInt32(Id)

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Mike said:
I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
message CarList.Items[Id].Selected = true;


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how can
I get all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.


 
A

Alex Meleta

Hi Mike,

So, that means that length of Id array greater then CarList.Items's length.
See my code in the early message with validation by Id.Contains or such methods.

for (int i = 0; i < CarList.Items.Count; i++)
{
if (Id.Contains(... some checks and convertion... CarList.Items.Value )
{
CarList.Items.Selected = true;
}
}

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> its an interger:
M>
M> I have this:
M>
M> for (int i = 0; i < Id.Length; i++)
M> {
M> CarList.Items[Convert.ToInt32(ID)].Selected = true;
M> }
M> and I get this when I run it now:
M> Index was out of range. Must be non-negative and less than the size
M> of the
M> collection.
M> Parameter name: index
M> and its referring to this line:
M> CarList.Items[Convert.ToInt32(ID)].Selected = true;
M> the id value is coming through as (1,2,3,4,5,6,7)
M> then I split it to remove the comma's
M> message M>
What's the type of Id? If it is integer, the code should compile.
If it
is string, convert it first to integer as
System.Convert.ToInt32(Id)
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
in
message CarList.Items[Id].Selected = true;

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want
to
highlight (select) all the items in the listbox were the value
equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how
can
I get all of them selected?
The listbox selectionMode= mulitple
for (int i = 0; i < Id.Length; i++)
{
CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the
array
selected. I want all of the values passed in selected.
 
M

Mike

the numbers being passed in aren't index numbers there values of the data
being passed.

how this work is, I have a gridview and has a salesman, and cars listed next
to the salesperson

example:
salesman: cars
smith BMW, Lexus, Mercedes, Volvo


I have a button that allows me to edit the sales person, I then I need to
carry over BMW, Lexus, Merecedes, Volvo to my edit screen and have those
cars highlighted in my listbox.

The value of the cars are being passed(BMW = 1, Lexus = 4, Mercedes = 7,
Volco = 5) like that, So I need to grab that value, and select each one in
my listbox.




Patrice said:
Ah great ! This error message means that the index is out of the allowed
bounds. For example is 1 the first element ? It should be 0.

Depending what you are doing you may want to use 0,1,2,3,4,5,6 instead of
1,2,3,4,5,6,7 if you are using the order of those elements in the
collection.

--
Patrice

Mike said:
its an interger:

I have this:

for (int i = 0; i < Id.Length; i++)
{
CarList.Items[Convert.ToInt32(ID)].Selected = true;
}

and I get this when I run it now:
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

and its referring to this line:
CarList.Items[Convert.ToInt32(ID)].Selected = true;

the id value is coming through as (1,2,3,4,5,6,7)
then I split it to remove the comma's

Eliyahu Goldin said:
What's the type of Id? If it is integer, the code should compile. If
it is string, convert it first to integer as
System.Convert.ToInt32(Id)

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I can't even compile with this:

CarList.Items[Id].Selected = true;

I get error messages
message CarList.Items[Id].Selected = true;


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I want to
highlight (select) all the items in the listbox were the value equals
(1,2,3,4,5,6,7). I'm able to only get the last value selected, how
can I get all of them selected?
The listbox selectionMode= mulitple

for (int i = 0; i < Id.Length; i++)
{

CarList.SelectedValue = Id;
}
this is only getting me the last value being passed in from the array
selected. I want all of the values passed in selected.


 
A

Alex Meleta

Hi Mike,

It's because the code should be adapted to your circumstances. e.g. like
this:

List<int> Id = new List<int>();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (Id.Contains(Convert.ToInt32(ListBox1.Items.Value))) // be sure
that all items are integer or do some type check
{
ListBox1.Items.Selected = true;
}
}

So you should just know that particular item can be selected only by <List>.Items[<index>].Selected
in the multiple mode. Matching and other background code depends on you application.

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> this is driving me nuts,
M> I tried the code snippet and nothing is highlighted. Its not even
M> executing
M> the (IF) logic of the code.
M> for (int i = 0; i < CarList.Items.Count; i++)
M>
{
if (Id.Contains(CarList.Items.Value)) // I dunno the type of Id
array
(probably string), change it to you needs {
CarList.Items.Selected = true;
}
}

M> M>
Hi Mike,

Ou, yes, it's for select all items.
However, the common idea was to use CarList.Items[<index>].Selected
to
select particular item and CarList.Items[<index>].Text to determinate
the
value to match.
E.g. you can chance the code to something like this:
for (int i = 0; i < CarList.Items.Count; i++)
{
if (Id.Contains(CarList.Items.Value)) // I dunno the type of Id
array
(probably string), change it to you needs {
CarList.Items.Selected = true;
}
}
Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
M> if the values are coming in as 1,2,3,4,5,6,7, how would the
foreach
M> work? I've tried that and it didn't work due to the comma's, so I
had
M> to split it and I'm getting the last one passed (7) selected
M> M>
Hi Mike,

Use foreach(ListItem listItem in CarList.Items) { listItem.Selected
= true; } instead.

CarList.Selected<someting> works with one item only.

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
M> I'm passing an array to a listbox such as (1,2,3,4,5,6,7), I
want
to
M> highlight (select) all the items in the listbox were the value
equals
M> (1,2,3,4,5,6,7). I'm able to only get the last value selected,
how
M> can I get
M> all of them selected?
M> The listbox selectionMode= mulitple
M> for (int i = 0; i < Id.Length; i++)
M> {
M> CarList.SelectedValue = Id;
M> }
M> this is only getting me the last value being passed in from the
array
M> selected. I want all of the values passed in selected.
M>
 
A

Alex Meleta

ok. so, I've made some extra comments to the code.

List<int> Id = new List<int>(); // imagine that it's a int array

// Iterates throught ListBox1.Items to find appropriate one
foreach(ListItem listItem in CarList.Items)
{
// Seek the listItem.Value in the Id's array (you can change
this code as you need).
if (Id.Contains(Convert.ToInt32(listItem.Value))) // or (carValue
= listItem.Value) in case of single one
{
// If value is found then select the item
listItem.Selected = true;
}
}

Hopefully, it helps

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> the numbers being passed in aren't index numbers there values of the
M> data being passed.
M>
M> how this work is, I have a gridview and has a salesman, and cars
M> listed next to the salesperson
M>
M> example:
M> salesman: cars
M> smith BMW, Lexus, Mercedes, Volvo
M> I have a button that allows me to edit the sales person, I then I
M> need to carry over BMW, Lexus, Merecedes, Volvo to my edit screen and
M> have those cars highlighted in my listbox.
M>
M> The value of the cars are being passed(BMW = 1, Lexus = 4, Mercedes
M> = 7, Volco = 5) like that, So I need to grab that value, and select
M> each one in my listbox.
M>
 
M

Mike

this is what i actaully started out with and it only selected the last item
in the 'array' in my list box.
the only thing I didn't have was: but everything else I had,then that didn't
work so I tried some other things and got the same result or nothing got
selected. so.



List<int> Id = new List<int>();

also, I've been looking through google and found a posting that was doing
the same thing and the response was to use

ListBox.SetSelected(<value>, true);

now if I add ListBox.SetSelected(<value>, true) I get this:

Error 4 'System.Web.UI.WebControls.ListBox' does not contain a definition
for 'SetSelected'

why? I even copied some code from MSDN with this and that errored out as
well with the same error
 
M

Mike

I just saw were listbox.setselected is for winform list box not asp.net

Mike said:
this is what i actaully started out with and it only selected the last
item in the 'array' in my list box.
the only thing I didn't have was: but everything else I had,then that
didn't work so I tried some other things and got the same result or
nothing got selected. so.



List<int> Id = new List<int>();

also, I've been looking through google and found a posting that was doing
the same thing and the response was to use

ListBox.SetSelected(<value>, true);

now if I add ListBox.SetSelected(<value>, true) I get this:

Error 4 'System.Web.UI.WebControls.ListBox' does not contain a definition
for 'SetSelected'

why? I even copied some code from MSDN with this and that errored out as
well with the same error



Alex Meleta said:
ok. so, I've made some extra comments to the code.

List<int> Id = new List<int>(); // imagine that it's a int array
// Iterates throught ListBox1.Items to find appropriate one
foreach(ListItem listItem in CarList.Items)
{
// Seek the listItem.Value in the Id's array (you can change
this code as you need).
if (Id.Contains(Convert.ToInt32(listItem.Value))) // or
(carValue = listItem.Value) in case of single one
{
// If value is found then select the item
listItem.Selected = true;
}
}

Hopefully, it helps

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



M> the numbers being passed in aren't index numbers there values of the
M> data being passed.
M> M> how this work is, I have a gridview and has a salesman, and cars
M> listed next to the salesperson
M> M> example:
M> salesman: cars
M> smith BMW, Lexus, Mercedes, Volvo
M> I have a button that allows me to edit the sales person, I then I
M> need to carry over BMW, Lexus, Merecedes, Volvo to my edit screen and
M> have those cars highlighted in my listbox.
M> M> The value of the cars are being passed(BMW = 1, Lexus = 4,
Mercedes
M> = 7, Volco = 5) like that, So I need to grab that value, and select
M> each one in my listbox.
M>
 
M

Mike

I got this to work finally. My code was actually correct, I just never bound
the listbox, so when my foreach loop was executed, it had no data to loop
through. Thouh I was seeing the data in the listbox, it was cached, so once
I called my grid.databind() it worked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top