Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
In my project I generate a list box during runtime. I give a title 'SELECT BLOCK' to that list box at zero index. Can I give a diff color to that title. My code:
               DataSet ds1 = new DataSet("block");
                            da1.Fill(ds1, "block");
                            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                            {
                                adds1.Add(ds1.Tables[0].Rows[i].ItemArray[0].ToString());
                                string name1 = ds1.Tables[0].Rows[i]["block_name"].ToString();
                            }
                            adds1.Sort((x, y) => string.Compare(x, y));
                            adds1.Insert(0, "SELECT BLOCK");
                            ListBox1.DataSource = adds1;
                            ListBox1.DataBind();
                            ListBox1.Visible = true;
Can anyone advice?
Regards.
Posted 16-Jan-13 3:27am
Edited 16-Jan-13 4:12am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I would use a ListView[^] instead of the ListBox.
The ListView provides column titles and allows to change their attributes.
  Permalink  
Comments
Espen Harlinn at 17-Jan-13 16:57pm
   
Yes! :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Add this line after you bind your ListBox:
ListBox1.DataBind();
ListBox1.Visible = true;
if(ListBox1.Items.Count>0)
ListBox1.Items[0].Attributes.Add("style", "background-color:Red;");
or
ListBox1.DataBind();
ListBox1.Visible = true;
if(ListBox1.Items.Count>0)
ListBox1.Items[0].Attributes.Add("style", "color:Red;");
  Permalink  
v3
Comments
S.Rajendran from Coimbatore at 16-Jan-13 10:20am
   
I did as you said like follows:
 
adds1.Sort((x, y) => string.Compare(x, y));
adds1.Insert(0, "SELECT BLOCK");
ListBox1.DataSource = adds1;
ListBox1.Items[0].BackColor.Attributes.Add("style", "background-color:Red;");
ListBox1.DataBind();
 
but get error like:
'System.Web.UI.WebControls.ListItem' does not contain a definition for 'BackColor' and no extension method 'BackColor' accepting a first argument of type 'System.Web.UI.WebControls.ListItem' could be found (are you missing a using directive or an assembly reference?)
 
regards
Zafar Sultan at 16-Jan-13 10:48am
   
Add that code after databinding. Check my updated reply.
Espen Harlinn at 17-Jan-13 16:57pm
   
5'ed!
Zafar Sultan at 18-Jan-13 2:30am
   
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 OriginalGriff 230
2 DamithSL 175
3 Dave Kreskowiak 120
4 Zoltán Zörgő 114
0 OriginalGriff 7,740
1 DamithSL 5,694
2 Sergey Alexandrovich Kryukov 5,404
3 Maciej Los 5,061
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100