如何在列表框中为零索引标题着色 [英] How to color zero index title in a list box
本文介绍了如何在列表框中为零索引标题着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我在运行时生成一个列表框。我给零索引处的列表框标题''SELECT BLOCK''。我可以为该标题赋予差异颜色。我的代码:
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.
推荐答案
我会使用 ListView [ ^ ]而不是ListBox
。
ListView
提供列标题并允许更改其属性。
I would use a ListView[^] instead of theListBox
.
TheListView
provides column titles and allows to change their attributes.
绑定ListBox后添加此行:
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;");
或
ListBox1.DataBind();
ListBox1.Visible = true;
if(ListBox1.Items.Count>0)
ListBox1.Items[0].Attributes.Add("style", "color:Red;");
这篇关于如何在列表框中为零索引标题着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文