By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

listbox databind and mutli-select by code

P: n/a
Hello,

(I'm french, so my english isn't very good)

I use a dataTable to fill a listbox (listbox databinded). This listbox
has a valuemember (integer) and a displaymember (string) differents.

In my code, I must select one or more items in the listbox. The only
information I have is the list of items corresponding to the
valuemember that I must select.

I try many solution : using integer, dataRowView to find the listbox
item to select but in all case, I have errors. I don't know what I must
do ????

Thanks for all response
Steph

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

i don't know if this will help, because I am not 100% sure of the
problem...sorry

but here is a code example (C#)...basically you pass in your
defaultvalue, your datatable and your list box...

public void fillList(string defaultValue, DataTable dtInfo, ListBox
dlReturn)
{
for(int i=0; i < dtInfo.Rows.Count; i++)
{
ListItem dListItem = new ListItem();
dListItem.Value = dtInfo.Rows[i].ItemArray[0].ToString();
dListItem.Text = dtInfo.Rows[i].ItemArray[1].ToString();
if((defaultValue.ToString() != "") &&
(dtInfo.Rows[i].ItemArray[0].ToString() == defaultValue.ToString()))
{
dListItem.Selected = true;
}
else if(defaultValue.ToUpper().ToString() == "ALL")
{
dListItem.Selected = true;
}
dlReturn.Items.Add(dListItem);
}
}

Then in your calling code you do:
fillList("ALL",dsInfo.Tables[3],lboxInfo);//lboxInfo is the name/id of
the ListBox Control
In the above example, I only have one default value but you can pass in
an array (then do a second loop within the For statement to check if you
need to select the listitem) or you can have the DataTable object
contain a Boolean column that indicates if the value should be
selected.

Hope this helps.

Ralph
--
rviray
------------------------------------------------------------------------
rviray's Profile: http://www.msusenet.com/member.php?userid=4211
View this thread: http://www.msusenet.com/t-1871083080

Nov 21 '05 #2

P: n/a

DURRRR....sorry about the c# example...forgot I was in the VB
section...I will post up a VB example shortly....but in the mean time,
you can easily convert the example to VB...
--
rviray
------------------------------------------------------------------------
rviray's Profile: http://www.msusenet.com/member.php?userid=4211
View this thread: http://www.msusenet.com/t-1871083080

Nov 21 '05 #3

P: n/a
This example doesn't resolve my problem but it takes me an idea to
resolve my problem and not it's work well.

In fact I looped on values array to select in my listbox and I tried to
find which items in the listbox match the current item of my values
array.
Now I loop on the listbox and if the current item of the listbox is
contained in my values array, I mark it as selected. The properties and
methods of arrays avoid me to do two loops. It's more simply in this
manner.

Thanks

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.