I have a listbox and the listitems are populated from database.
The listitems are the students names of a school.
Now looking at the items, I cannot distinguish between the names of students of different classes. I want to make some change in the listbox items as example, make different font color for different classes students names in the listbox.
At least it will be such that the background of the listitems of student names of class 1 will have red background and then the background of the listitems of student names of class 2 will have green background and then again the background of the listitems of student names of class 3 will have red again and so on........
The number of students in each class is different from other classes.
Pls help.

I havn't used a listbox but I use gridview and detailsview frequently. When I wish to change the formatting of a particular type of field I will add a hidden field then check values in the code behind. Here is a brief ex.

           <asp:GridView ID="GridView1"    DataKeyNames="PROPERTY_NAME" BorderColor="#1E1E1E" runat="server" AllowPaging="True" 
        AllowSorting="True" PageSize="15"  CssClass="gridview" 
            AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
        <Columns>
                <asp:TemplateField ItemStyle-Width="250px" HeaderText="Name" SortExpression="Property_Name">
                    <ItemTemplate>
                        <asp:Label ID="lblPropertyName" Text='<%# EVAL("PROPERTY_NAME") %>' runat="server"></asp:Label>
                        <asp:HiddenField ID="hidNoLongerManage" Value='<%# eval("NO_LONGER_MANAGE") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>

            <asp:BoundField DataField="Property_ID" HeaderText="Code" 
                SortExpression="Property_ID" />
            <asp:BoundField DataField="Prop_Abbr" HeaderText="Abbreviation" 
                SortExpression="Prop_Abbr" />

        </Columns>
        <SelectedRowStyle CssClass="gridviewSelected" />
        <RowStyle CssClass="gridviewRow" />
        <PagerStyle CssClass="gridviewPager" />         
        <PagerSettings Mode="NumericFirstLast" />
    </asp:GridView>

and the codebehind:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    Try

        If chkShowInactive.Checked = True And CType(e.Row.FindControl("hidNoLongerManage"), HiddenField).Value = 1 Then
            e.Row.BackColor = Drawing.Color.Khaki
            e.Row.ForeColor = Drawing.Color.Gray
        ElseIf chkShowInactive.Checked = False And CType(e.Row.FindControl("hidNoLongerManage"), HiddenField).Value = 1 Then
            e.Row.Visible = False
        End If


    Catch ex As Exception

    End Try

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.