Listbox with spaces from SQL query

D

dachrist28

I am trying to populate a listbox with data that is pulled from a sql
query. Here is my C# code behind the page:
sqlConn1.Open();
SqlCommand sqlComm2 = new SqlCommand("SElECT
replace((mc.MAINTENT.id + space(20-len(mc.maintent.id))+
left(mc.MAINTENT.aenm,50) +
space(50-len(left(mc.maintent.aenm,50)))+mc.mtntype.id),' ','&nbsp') as
Description, mc.maintent.mtnoi FROM mc.MAINTENT INNER JOIN mc.MTNTYPE
ON mc.MAINTENT.mtctyp_oi = mc.MTNTYPE.mtoi WHERE (mc.MAINTENT.id >=
'F92') AND (mc.MAINTENT.aenm LIKE '%%') AND (mc.MTNTYPE.id LIKE '%%')
ORDER BY mc.MAINTENT.id", sqlConn1);
SqlDataReader sqlRead2 = sqlComm2.ExecuteReader();
lbEntities.DataSource = sqlRead2;
lbEntities.DataTextField = "Description";
lbEntities.DataValueField = "mtnoi";
lbEntities.DataBind();
sqlConn1.Close();

As you can see, I want to have white spaces in my listbox so the
columns appear nicely. I am replacing the spaces with &nbsp in my
query. The problem is that when the listbox displays, rather than
spaces I get    etc. How do I pass the &nbsp from the query
to display the correct spaces in my listbox?
 
A

Andy

Listboxes are converted into MFC droplist controls in Internet
Explorer; they do not display HTML code themselves.

The best you can do is apply a class in a Cascading Style Sheet (or set
the style attribute on the listbox) to have the text in the box render
in a fixed pitch font. You can then use regular spaces to align its
entries.
 
U

usenet.junk.000

I am trying to populate a listbox with data that is pulled from a sql
query. Here is my C# code behind the page: ....
As you can see, I want to have white spaces in my listbox so the
columns appear nicely. I am replacing the spaces with &nbsp in my
query. The problem is that when the listbox displays, rather than
spaces I get    etc. How do I pass the &nbsp from the query
to display the correct spaces in my listbox?

You'll need to use a fixed size font (Courier New for example) in your
listbox. Then replace the spaces in your listitems with an HTML entity
that will actually render as spaces in the listbox (not so easy, as you
may have found). Here's how to get the entity:

System.IO.StringWriter sWriter = new System.IO.StringWriter();
//get the HTML space ready.
HttpContext.Current.Server.HtmlDecode(" ", sWriter);
string sHTMLSpace = sWriter.ToString();

Then you need to replace all the spaces that you just pulled back from
the server with sHTMLSpace. I believe ASP.NET will let you do
something like this after databinding:

foreach (ListItem li in lbEntities.Items)
{
li.Text = li.Text.Replace(" ", sHTMLSpace);
}

Anyway, that should set you on the right path.

Matt
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top