?
Solved

Coloring datagrid

Posted on 2005-03-23
27
Medium Priority
?
231 Views
Last Modified: 2010-04-07
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
Comment
Question by:ayha1999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 12
27 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13614993
<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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13615246
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13619939
Hi laotzi2000,

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

thanks in advance.

ayha
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:laotzi2000
ID: 13621755
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13622666
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13622754
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13622811
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13622904
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13624736
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13624758
you can have an else for all others.

if () then
else if() then
...
else
...
end if
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13624770
and you can make up a name for them. like
session("default") = ...
0
 
LVL 7

Author Comment

by:ayha1999
ID: 13624895
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13624951
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13625101
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
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 750 total points
ID: 13625305
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13636419
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13636482
I did not see you assign any color to mycolor anywhere.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 13636630
How can I assign colors to the array?

ayha
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13636636
dim mycolor(10) as string
mycolor(0)="red"
...
mycolor(9)="blue"

0
 
LVL 7

Author Comment

by:ayha1999
ID: 13636814
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13636864
mycolor(0) = "#FF99FF"
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13636888
or
e.item.forecolor = Color.FromName("cc66ff")
0
 
LVL 7

Author Comment

by:ayha1999
ID: 13639476
This give me same color to different categories.

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

ayha
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13639511
I'm just showing you how to use it.
You can do
e.Item.ForeColor = Color.FromName(mycolor(index))
0
 
LVL 7

Author Comment

by:ayha1999
ID: 13639927
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
 
LVL 7

Author Comment

by:ayha1999
ID: 13653794
Hi laotzi2000,

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

ayha
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13653980
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question