css toolkit adapters- apply css style to gridview row in code (c#)

I was trying to change the colour of a row in a gridview depending on data contained within using MyGridView_RowDataBound, and found a nice little change to the css adapters to allow this. Thought I should share.

Add the following code before "return className.Trim();" in the function GetRowClass(GridView gridView, GridViewRow row) in GridViewAdapter.cs. (I keep this in ~/App-Code/Adapters/)

if ((row.RowState == DataControlRowState.Normal))


className += row.CssClass; 


In your gridview css file add the colors you need EG:

.MyGridViewCSSselectorClass .AspNet-GridView table tbody tr.AspNet-GridView-Red td


background: Red;


.MyGridViewCSSselectorClass  .AspNet-GridView table tbody tr.AspNet-GridView-Green td


background: Green;


In your web page create a RowDataBound function. To over ride the alternate row style change the row state to normal on every row you wish to change, EG:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 



if (e.Row.RowType == DataControlRowType.DataRow)

foreach (Row in the gridview)


e.Row.RowState = DataControlRowState.Normal;

if (somethingIsTrue)

e.Row.CssClass = "AspNet-GridView-Red"; 

else if (somethingElseIsTrue)

e.Row.CssClass = "AspNet-GridView-Green";



catch (Exception ex)

throw ex;



hope this makes sense!

11/21/2007 12:34:15 PM
asp.net.css-control-adapters 905 articles. 0 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 0

Nice.  One suggestion would be to provide semantic names rather than visual names, for instance


This helps in the situation where someone decides they want non-error rows gray and error rows yellow.  Then you don't end up with this in your CSS file:

tr.AspNet-GridView-Red td: { color: yellow; }
tr.AspNet-GridView-Green td: { color: gray; }

Just a thought.  I've seen this happen.  :-)


Vine Type: Content Management with Standards in Mind. http://vinetype.com
12/14/2007 8:45:30 PM

Yes good point.

Colours should be assigned in the CSS not in the code. To further your point if another programmer creates another gridview he/she would have to assign the AspNet-GridView-Red class to colour the row yellow!

What a mess!!

12/23/2007 6:46:47 PM

Similar Artilces:

Toggle Button, ToolKit CSS, CSS Adapters
First: There is no real point to this message, it's just me ranting about something that annoyed me in the ajax.net samples online. All in all I think the teams working on ajax.net and css are doing a splendid job, and Atlas works well in most cases, and for most needs. However, my rent is more about some whining about details.  Now, here is something that sort of just irked me. The same folks who do AJAX.NET are the same folks who are doing CSS adapters. (assuming I am not mistaken on that). In any case, the image flickering on a small demo such as the ToggleButton is simpl...

CSS Friendly Control Adapters not support dynamically created rows in GridView?
I have a dynamically created row in GridView control. But it will not appear when I use CSS Friendly Control Adapter. I have to set adapterenabled=false to make it work. But I will loss all the table style in this way. Any better ideas to solve this problem? This is the code behind to add a new row.protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e) {GridViewRow insertedRow = new GridViewRow(GridView3.Rows.Count, GridView3.Rows.Count, DataControlRowType.DataRow, DataControlRowState.Normal); TableCell cell = new TableCell();cell.Text = " hello"; cel...

CSS Control Adapter for Gridview
I am tryin to style a gridview.My site is using CSS Friendly control adapter. I am new to this. Can u guys pls help me make the gridview look better. Every time i add a div tag,i get an error that div is not a property of the gridview.If i put it to envelope the gridview, nothin happens!! Thanks.  If you left the (ID) of the GridView as GridView1 then you should be able to find <div class="AspNet-GridView" id="GridView1"> in the source when you run the page.   You can use the class to style the GridView.  You should also be able to find ot...

Reagrding using FILTER in (MenuExample.css) CSS adapter classes for Menu control
Hello everyone, Whenever I use filter: in any of the CSS calss for the MENU control using CSS adapters, it only renders the first level of the menu, all subsequent levels are not even displayed. I want to use filter for some transparency effect, but using it is not rendering any child menu's. Following is a example - .PrettyMenu .AspNet-Menu-Vertical { position:absolute; top: 87px; left: 0px; width:200px; z-index: 300; filter:alpha(Opacity=50); } Now it only shows first level and rest of the menus are not displayed, if I remove filter, it works fine but without transparency. Rep...

Changing style of GridView Row in code-behind on a CSS-Friendly Gridview
Hey folks, here's the problem: I have a gridview that displays messages, and I want the unread messages to be displayed as bold.  Simple, right?  One problem: while you can set the CssClass in the code-behind to accomplish this, you can't set the CssSelectorClass. Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound Dim msg As Message = CType(e.Row.DataItem, Message) If e.Row.RowType = DataControlRowType.DataRow Then If Not msg...

What is the advantage of the CSS control adapter for the gridview?
 The Grid View has built in styling capabilities with the "HeaderStyle-CssClass" and the "ItemStyle-CssClass" attributes.  Why sould someone want to use the CSS Friendly Control Adapter for this if it has this built in capability?  The html that it produces via the adapter doesn't seem that much better than the html that it produces normally.  Is the only reason to use the adapter on the Grid View so that the output can be manually changed?  For instance,  the table that is put out by the Grid View can canged into <divs>.  ...

CSS Menu Adapter + Ajax Control Toolkit
Hi all, I am facing a problem while using CSS control adapters in my Ajax enabled website.Could anybody please suggest me how to move forward in this scenario.  Thanks Anuradha Well, first- what problems are you facing?"If you have knowledge, let others light their candles in it." — Margaret Fuller...

Applying styles to GridView control in external CSS
Alright, I've looked everywhere but I still can't seem to find a simple explanation of how to apply styles to a GridView control. I just need to know the attributes of a GridView control that can be customized. For example, I already figured out that I can customize the "caption" attribute: my GridView control has 'CssClass = "thisClass"' and my stylesheet has '.thisClass caption { color:Blue: }'I need to get the names of the attributes of the GridView control such that I can eliminate the border and apply other formatting styles. Thanks a bunch in advance. Can anybody offer some insight?...

Menu Divider or Seperator using CSS and or CSS adapters.
Great work on the Adapters.  I am attempting to create a menu with separator via CSS between specific items.  Please refer to http://forums.asp.net/thread/1449615.aspx for more detail with regards to plain (non css adapted) menus.  Is it possible using CSS adapters to specify a divider or separator between specific menu items, preferably using styles/css such as a border-bottom: 1px or even a 1 px high span/div tag with a background color?  Please note I don't want to apply this to all items in the menu, just specific ones.  Can this be done ...

Field css style / Label css style
What are the valid values to key in this fields through Novell Designer. Basically, I would like to change the border-color/background color of a SelectBox. I can't seem to find any documentation on this. Thanks in advance, Jason On 02/01/2009 08:35 AM, Jason Kee wrote: > What are the valid values to key in this fields through Novell Designer. > > Basically, I would like to change the border-color/background color of a > SelectBox. > > I can't seem to find any documentation on this. > > Thanks in advance, > Jason Basically you...

CSS control adapters
I've just seen another thread mentioning these which reminded me that I couldn't install the blighters. A couple of questions first tho.  The VSI file that I download won't load/install, it comes up with "string cannot have zero length".  ANybody any ideas on what may be going wrong? Second question is on how these get deployed on my remote server, will I be able to use them without access to the core asp library directories (if that makes sense). To fix the VSI installer problem, see this posting, http://forums.asp.net/thread/1492405.aspx. To deploy the adapters you do not ...

GridView + CSS Adapter?
Hi Developer,I just wondering, are u guys gonna build GridView based on CSS? is it already on your roadmap?thx...

CSS Control Adapters
I was thinking to implement menu like on www.auctiva.com using CSS Control Adapters. How would I do it?The enu would have two bars:1) Main bar - tabs 2 Second bar with submenu items, all links horizontallyRafal Zikbay.com - Free Classifieds (Search Over 5 mln jobs on Zikbay.com) DNN Direct Anybody can help?Rafal Zikbay.com - Free Classifieds (Search Over 5 mln jobs on Zikbay.com) DNN Direct ...

Is it possible to apply CSS Adapters to a single specific control?
Can CSS Adapters be applied to a single specific ASP.NET control, or must these CSS Controls be applied Project wide?Could someone provide an example / tutorial for a CSS Adapter being applied to a single ASP.NET control?Thanks in advance, Adam,You can disable the adapters on a per-control basis using AdapterEnabled="false", but in the documentation is remarked that this option is only experimental because the framework doesn't support disabling adapters on a per control basis. Also you can modify the CSSFriendlyAdapters.browser file and comment the controls you don't want to get adapted,...

Web resources about - css toolkit adapters- apply css style to gridview row in code (c#) - asp.net.css-control-adapters

Adapter - Wikipedia, the free encyclopedia
An electrical adapter may enable connection of a socket used in one region to a plug used in another by offering connections for the disparate ...

AC Adapter - Replacement Laptop AC Adapters
Online shopping for laptop ac adapters at the best price.Buy cheap ac adapters for your laptop computer.

M3 Adapter
Update the firmware of the M3i Zero GMP-Z003 card to support running in new 3DS (Ver. 4.1.0-8E, Ver. 4.1.0-8J, Ver. 4.1.0-8U) and DSi (Ver 1.4.4E, ...

Bluetooth RFCOMM / SDP connection to a RS232 adapter in android
The Android API provides examples of using listenUsingRfcommWithServiceRecord() to set up a socket and createRfcommSocketToServiceRecord() to ...

Mac App Store - Power Adapter Alarm
Get Power Adapter Alarm on the Mac App Store. See screenshots and ratings, and read customer reviews.

iPhone 4 Bumper + Universal Dock w/ DIY Adapter - Flickr - Photo Sharing!
You cannot use the iPhone 4 dock with the bumpers on. One solution is to use the universal dock without an adapter. This is not an optimal solution, ...

FINsix Compact Laptop Adapter at CES 2014 - Engadget - YouTube
Fed up with your bulky laptop power adapters? MIT spin-off FINsix has come up with a breakthrough technology that dramatically reduces both the ...

Apple's iPhone 5 'Lightning' adapters will retail for $35 and $45, not available until October
Apple's decision to change the iPhone 5's dock connector means you'll need an adapter or two.

Travel tips: Do I need to pack a travel adapter?
Today's travellers have flown the nest of cords and plastic bits, but there is one essential gadget that cannot be underestimated.

Microsoft unveils Chromecast-like Wireless Display Adapter
Microsoft has put its spin on Google's Chromecast with a device that lets people mirror what's on their PC, laptop or smartphone wirelessly onto ...

Resources last updated: 1/18/2016 4:08:42 PM