• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Coloring datagrid

Hi,

I am popoulating a datagrid with

ProductID ProductNmae Category etc.

How can give different colors to rows of each category. Colors can dynamic or manual, manual is preferred.

can anhone hlep me with samples.

ayha

0
ayha1999
Asked:
ayha1999
  • 14
  • 12
1 Solution
 
b1xml2Commented:
<asp:DataGrid id="DataGrid1" runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3">
<AlternatingItemStyle BackColor="#DCDCDC"></AlternatingItemStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
<Columns>

</Columns>
</asp:DataGrid>

Recommendation: Use Visual Studio.NET
0
 
laotzi2000Commented:
You can override the ItemDataBound event handler of datagrid
in this event handler, you can get the category value from e.item.cells(index of category)
you can change its color according to this value by setting
e.item.forecolor
or
e.item.backcolor
0
 
ayha1999Author Commented:
Hi laotzi2000,

could u pls. give complete itemdatabound scirpt for assigning different colors for categories.

thanks in advance.

ayha
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
laotzi2000Commented:
something like the following:

if (e.item.cells(2) == "Cat1") then
  e.item.forecolor = "red"
  e.item.backcolor = "blue"
else if (e.item.cells(2) == "Cat2") then
  e.item.forecolor = "white"
  e.item.backcolor = "Black"
else
  e.item.forecolor = "blue"
  e.item.backcolor = "red"
end if
0
 
ayha1999Author Commented:
Hi  laotzi2000,

So I have to mention colors for each category. Is there is any possibility to assign dynamic colors (different colors) for each category?

thanks in advance.

ayha
0
 
laotzi2000Commented:
You can build a map that maps a category to a color
in itembound, you can lookup your category in this map
and assign the proper color.
You can modify this map at run time to assign a different
color to a specific category.

If you don't have many categories, you can just
use session variables. Like:
session("cat1") = "red"
session("cat2") = "blue"
0
 
ayha1999Author Commented:
Hi laotzi2000,

Sorry. Your last post is not clear on how to do dynamic assigning of colors if have many categories.  could u pls give a sample.

thanks in advance.

ayha
0
 
laotzi2000Commented:
for example,
when the page load the first time you can assign colors
session("cat1")="red"
session("cat2")="blue"

and some time later, you want to change that
session("cat1")="white"
session("cat2")="black"
0
 
ayha1999Author Commented:
Hi laotzi2000,

Suppose I don't know what categories are there, how can I assign dynamic colors to the categories?

I assume that the following is only for known categories.

if (e.item.cells(2) == "Cat1") then
  e.item.forecolor = "red"
  e.item.backcolor = "blue"

ayha
0
 
laotzi2000Commented:
you can have an else for all others.

if () then
else if() then
...
else
...
end if
0
 
laotzi2000Commented:
and you can make up a name for them. like
session("default") = ...
0
 
ayha1999Author Commented:
Hi laotzi2000,

Still I am not clear on how to get differnt colors for categories. I assue that your all posts assigns colors for only for known categories.

If I am wrong, could u pls. post the complete itemdatabound event so that I clear my doubt and close the question.

thanks in advance.

ayha
0
 
laotzi2000Commented:
yeah, those are for known categories.
all unknown categories will be the same.
But if you don't know which categories it is, how can you assign a color for it?
0
 
ayha1999Author Commented:
Hi laotzi2000,

I think u get an idea about it with the following;

dim CurrentColor,oldCategory
dim myColors(10)
mycolor(0)="#ffffff"
mycolor(1)="#996699"
mycolor(2)="#CC99CC"
mycolor(3)="#669966"
mycolor(4)="#ffffff"
' Add all the colors here for 4-9

CurrentColor=""
CurrentIndex=0
oldCategory=""
while not rst.EOF and rowCount < rst.PageSize
          rowCount = rowCount + 1
If(oldCategory<>rst("Category")) Then
     if(currentIndex=9 ) then
               currentIndex=0
    else
               currentIndex=CurrentIndex +1

     end if
CurrentColor=myColor(CurrentIndex)
End If %>

<tr bgcolor=<%=CurrentColor%>>
<td align="middle" width="70"><%=server.HTMLEncode(rst("PartNo"))%>&nbsp;</td>
<td align="left" width="153"><%=server.HTMLEncode(rst("ProductName"))%>&nbsp;</td>
<td align="left" width="95"><%=server.HTMLEncode(rst("Category"))%>&nbsp;</td>
<td width="30" align="middle"><%=rst("Qty")%>&nbsp;</td></tr>

<%
oldCategory=rst("Category")
rst.MoveNext
wend

%>

But I don't know how it can be achieved in itemdatabount event.

ayha
0
 
laotzi2000Commented:
if session("index") is nothing then
  session("index") = 0;
end if

if session("category") is nothing then
  session("category") = e.item.cells(2).text
end if

int index = session("index")
if(e.item.cells(2).text <> session("category") then
  index = (index + 1) mod 10
  session("category") = e.item.cells(2).text
end if

e.item.forecolor = mycolor(index)

session("index") = index
0
 
ayha1999Author Commented:
Hi laotzi2000,

I tried the following but I don't get any colors for the rows.  could u pls. check what is wrong?

Select Case e.Item.ItemType
Case ListItemType.Item
                Dim index As Integer = Session("index")
                Dim lblCat As String = CType(e.Item.FindControl("lblCategory"), Label).Text

                If Session("index") Is Nothing Then
                    Session("index") = 0
                End If

                If Session("category") Is Nothing Then
                    Session("category") = lblCat
                End If

                If lblCat <> Session("category") Then
                    index = (index + 1) Mod 10
                    Session("category") = lblCat
                End If

                Dim mycolor()
                e.Item.ForeColor = mycolor(index)
                Session("index") = index
End Select

ayha
0
 
laotzi2000Commented:
I did not see you assign any color to mycolor anywhere.
0
 
ayha1999Author Commented:
How can I assign colors to the array?

ayha
0
 
laotzi2000Commented:
dim mycolor(10) as string
mycolor(0)="red"
...
mycolor(9)="blue"

0
 
ayha1999Author Commented:
Still I don't get colors for categories.

...
Dim mycolor(10)
                mycolor(0) = "FF99FF"
                mycolor(1) = "FFCC66"
                mycolor(2) = "CC99FF"
                mycolor(3) = "99CCFF"
                mycolor(4) = "99FF66"
                mycolor(5) = "33FFCC"
                mycolor(6) = "9999CC"
                mycolor(7) = "FF99FF"
                mycolor(8) = "CC99FF"
                mycolor(9) = "9999CC"

                e.Item.ForeColor = mycolor(index)
                Session("index") = index
ayha
0
 
laotzi2000Commented:
mycolor(0) = "#FF99FF"
0
 
laotzi2000Commented:
or
e.item.forecolor = Color.FromName("cc66ff")
0
 
ayha1999Author Commented:
This give me same color to different categories.

  e.Item.ForeColor = Color.FromName("FF99FF")

ayha
0
 
laotzi2000Commented:
I'm just showing you how to use it.
You can do
e.Item.ForeColor = Color.FromName(mycolor(index))
0
 
ayha1999Author Commented:
Hi laotzi2000,

I tried the following but I don't get any colors for categories.

  Case ListItemType.Item
                Dim index As Integer = Session("index")
                Dim lblCat As String = CType(e.Item.FindControl("lblCategory"), Label).Text

                If Session("index") Is Nothing Then
                    Session("index") = 0
                End If

                If Session("category") Is Nothing Then
                    Session("category") = lblCat
                End If

                If lblCat <> Session("category") Then
                    index = (index + 1) Mod 10
                    Session("category") = lblCat
                End If

                Dim mycolor(10)
                mycolor(0) = "FF99FF"
                mycolor(1) = "FFCC66"
                mycolor(2) = "CC99FF"
                mycolor(3) = "99CCFF"
                mycolor(4) = "99FF66"
                mycolor(5) = "33FFCC"
                mycolor(6) = "9999CC"
                mycolor(7) = "FF99FF"
                mycolor(8) = "CC99FF"
                mycolor(9) = "9999CC"

                e.Item.ForeColor = Color.FromName(mycolor(index))
                Session("index") = index   Case ListItemType.Item
                Dim index As Integer = Session("index")
                Dim lblCat As String = CType(e.Item.FindControl("txtSerialNo"), Label).Text

                If Session("index") Is Nothing Then
                    Session("index") = 0
                End If

                If Session("category") Is Nothing Then
                    Session("category") = lblCat
                End If

                If lblCat <> Session("category") Then
                    index = (index + 1) Mod 10
                    Session("category") = lblCat
                End If

                Dim mycolor(10)
                mycolor(0) = "FF99FF"
                mycolor(1) = "FFCC66"
                mycolor(2) = "CC99FF"
                mycolor(3) = "99CCFF"
                mycolor(4) = "99FF66"
                mycolor(5) = "33FFCC"
                mycolor(6) = "9999CC"
                mycolor(7) = "FF99FF"
                mycolor(8) = "CC99FF"
                mycolor(9) = "9999CC"

                e.Item.ForeColor = Color.FromName(mycolor(index))
                Session("index") = index

ayha
0
 
ayha1999Author Commented:
Hi laotzi2000,

I am wating for your valuable reply so as to close the question.

ayha
0
 
laotzi2000Commented:
Can you debug it a little bit to see
1) this code get called?
2) lblCat has the correct value?
3) index has the correct value?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 14
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now