Thursday, March 22, 2012

ListBox and/or SQL/ADO question

I am populating listbox with a datreader.
DateReader sqlcommand is dynamic, based on several user selections
Listbox DataValueField is set to the tables primary key
Listbox DataTextField is multiple fields concatentated from SQL statement
For example SELECT [field1] + ' ' + [field2] AS showText...
Listbox would show the following
Joe Smith...
Mary Smith ...
John Doe...

I want to include a "row number" in the DataTextField as such
Listbox would show the following
1. Joe Smith...
2. Mary Smith...
3. John Doe...

Is there a way I can use an experssion in my SQL statement to create these
values or include the Listbox ListItem value as part of the DataTextField.

Thanks in advance,
PaulForgot to mention...
I need to use a ListBox because I need multiselect capabilities for the
items, therefore I don't thing DataGrid, DataList, or Repeater will work for
my needs.
I think you're going to have to use a DataTable instead of a DataReader,
since it looks like you're wanting to manipulate the data prior to using it
as a Data Source for your ListBox. The DataTable is disconnected, and holds
all the results of the query, instead of only one row at a time. You can
therefore manipulate the data, change the order of it, filter it, add
columns (for example, you might want to add a column with a unique
identifier for the "value" property of your List Items), etc.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

"Paul W" <pdweb4444@.comcast.net> wrote in message
news:_tgjb.780003$Ho3.206558@.sccrnsc03...
> I am populating listbox with a datreader.
> DateReader sqlcommand is dynamic, based on several user selections
> Listbox DataValueField is set to the tables primary key
> Listbox DataTextField is multiple fields concatentated from SQL statement
> For example SELECT [field1] + ' ' + [field2] AS showText...
> Listbox would show the following
> Joe Smith...
> Mary Smith ...
> John Doe...
> I want to include a "row number" in the DataTextField as such
> Listbox would show the following
> 1. Joe Smith...
> 2. Mary Smith...
> 3. John Doe...
> Is there a way I can use an experssion in my SQL statement to create these
> values or include the Listbox ListItem value as part of the DataTextField.
> Thanks in advance,
> Paul
Thanks for the insight Kevin,
I was thinking of speed with the datareader, but I am not popluating the
listbox with a large number of records, so your solution should work.
Paul

0 comments:

Post a Comment