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.

8 Years
Discussion Span
Last Post by MichaelWClark

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">
                <asp:TemplateField ItemStyle-Width="250px" HeaderText="Name" SortExpression="Property_Name">
                        <asp:Label ID="lblPropertyName" Text='<%# EVAL("PROPERTY_NAME") %>' runat="server"></asp:Label>
                        <asp:HiddenField ID="hidNoLongerManage" Value='<%# eval("NO_LONGER_MANAGE") %>' runat="server" />

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

        <SelectedRowStyle CssClass="gridviewSelected" />
        <RowStyle CssClass="gridviewRow" />
        <PagerStyle CssClass="gridviewPager" />         
        <PagerSettings Mode="NumericFirstLast" />

and the codebehind:

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


        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

Edited by Reverend Jim: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.