473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Datagrid - set row colour in code

Does anybody know of any examples on how to set both the row colour and the
alternate row colour of a datagrid in code?

I need to do this at runtime, probably in the page load event or something
similar.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
Mar 11 '08 #1
2 4372
The custom is to do it in RowDataBound event although RowCreated or
PreRender can be used as well.

Here is an example:

protected void myGrid_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch (e.Row.RowState)
{
case DataControlRowState.Normal:
e.Row.BackColor = Color.Blue;
break;
case DataControlRowState.Alternate:
e.Row.BackColor = Color.Green;
break;
case DataControlRowState.Selected:
e.Row.BackColor = Color.Red;
break;
}
}
}
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Phil Johnson" <Ph*********@discussions.microsoft.comwrote in message
news:E4**********************************@microsof t.com...
Does anybody know of any examples on how to set both the row colour and
the
alternate row colour of a datagrid in code?

I need to do this at runtime, probably in the page load event or something
similar.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com

Mar 11 '08 #2
Thanks for your help again Eliyahu, much appreciated.

--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
"Eliyahu Goldin" wrote:
The custom is to do it in RowDataBound event although RowCreated or
PreRender can be used as well.

Here is an example:

protected void myGrid_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch (e.Row.RowState)
{
case DataControlRowState.Normal:
e.Row.BackColor = Color.Blue;
break;
case DataControlRowState.Alternate:
e.Row.BackColor = Color.Green;
break;
case DataControlRowState.Selected:
e.Row.BackColor = Color.Red;
break;
}
}
}
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Phil Johnson" <Ph*********@discussions.microsoft.comwrote in message
news:E4**********************************@microsof t.com...
Does anybody know of any examples on how to set both the row colour and
the
alternate row colour of a datagrid in code?

I need to do this at runtime, probably in the page load event or something
similar.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com


Mar 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ZaGras | last post by:
does anybody know make the column in the datagrid with different background colour, meaning that column1 is blue in colour, column2 is yellow in colour..?
1
by: JD | last post by:
How do i get rid of the 1st column that appears in a datagrid? I do not want this? Also how do i change the colour of the background of a cell when the user selects it. i.e for the first coloumn...
0
by: Oldhandandy | last post by:
I'm trying to create a multiply table (SQL) datagrid which updates fields in the primary table. The primary table holds diluted information such as a colour id, e.g. colour id for black is 5, on the...
1
by: Alex | last post by:
Hi all... I have a DataGrid bound to a DataTable. My DataTable gets updated programmatically as it contains the status of a number of worker threads. Each "main" worker thread launches a...
4
by: SimonH | last post by:
Hi everyone, If anyone can help me with this I would be really really greatful. I have a custom object that I can't change. Two of the properties (hair colour and eye colour) are of type...
1
by: Simon Harris | last post by:
Hi All, Ok, I have 6 datagrids, populated from 1 dataset. Each datagrid has 3 columns. There are 18 columns in the dataset. I have written a sub to change the background colour of the row in...
1
by: Leo | last post by:
Can someone tell me how to make the sorting triangle on datagrid column header always show up? And which property can change the color of the triangle ? The default color is white. Thanks
0
by: satish | last post by:
I have a table with two columns field1 int,Field2 and added a column to datagrid on runtime sum (field1+field2) ,upon changing the data in either of the fileds the sum field or column too changes ....
1
by: satish | last post by:
I have a table with two columns field1 int,Field2 and added a column to datagrid on runtime sum (field1+field2) ,upon changing the data in either of the fileds the sum field or column too changes ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.