css toolkit adapters- apply css style to gridview row in code (c#)http://forums.asp.net/t/1184845.aspx/1?css+toolkit+adapters+apply+css+style+to+gridview+row+in+code+c+Thu, 23 Feb 2012 06:58:56 -050011848452018579http://forums.asp.net/p/1184845/2018579.aspx/1?css+toolkit+adapters+apply+css+style+to+gridview+row+in+code+c+css toolkit adapters- apply css style to gridview row in code (c#) <p>I was trying to change the colour of a row in a gridview&nbsp;depending on data contained within using My<font size="2">GridView_RowDataBound, and found a nice little change to the css adapters to allow this. Thought I should share.</font></p> <p>Add the following code&nbsp;before &quot;<font color="#0000ff">return</font><font size="2"> className.Trim();&quot; in the function</font>&nbsp;GetRowClass(GridView gridView, GridViewRow row) in GridViewAdapter.cs. (I&nbsp;keep this in ~/App-Code/Adapters/)</p> <pre class="prettyprint">if ((row.RowState == DataControlRowState.Normal)) { className &#43;= row.CssClass; }</pre><FONT size=2> <P>In your gridview css file add the colors you need EG:</P><FONT color=#a31515 size=2> <P>.MyGridViewCSSselectorClass</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>.AspNet-GridView</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>table</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>tbody</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>tr.AspNet-GridView-Red</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>td</P></FONT><FONT size=2> <P>{</P></FONT><FONT color=#ff0000 size=2>background</FONT><FONT size=2>: </FONT><FONT color=#0000ff size=2>Red</FONT><FONT size=2>;</FONT></FONT><FONT size=2> <P>}</P></FONT><FONT color=#a31515 size=2> <P>.MyGridViewCSSselectorClass<FONT color=#000000 size=2>&nbsp;</FONT></FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>.AspNet-GridView</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>table</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>tbody</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>tr.AspNet-GridView-Green</FONT><FONT size=2> </FONT><FONT color=#a31515 size=2>td</P></FONT><FONT size=2> <P>{</P></FONT><FONT color=#ff0000 size=2>background</FONT><FONT size=2>: Green</FONT><FONT size=2>;</FONT></FONT><FONT size=2> <P>}</P></FONT> <P>In your web page create a <FONT size=2>RowDataBound<FONT size=2> function. To over ride the alternate row style change the row state to normal on every row you wish to change, EG:</FONT></FONT></P><pre class="prettyprint"><SPAN class=kwd>protected void</SPAN> GridView1_RowDataBound(<SPAN class=kwd>object</SPAN> sender, GridViewRowEventArgs e) { <SPAN class=kwd>try</SPAN> { <SPAN class=kwd>if</SPAN> (e.Row.RowType == DataControlRowType.DataRow) { <SPAN class=kwd>foreach</SPAN> (Row <SPAN class=kwd>in</SPAN> the gridview) { e.Row.RowState = DataControlRowState.Normal; <SPAN class=kwd>if</SPAN> (somethingIsTrue) e.Row.CssClass = <SPAN class=st>"AspNet-GridView-Red"</SPAN>; <SPAN class=kwd>else if</SPAN> (somethingElseIsTrue) e.Row.CssClass = <SPAN class=st>"AspNet-GridView-Green"</SPAN>; } } } <SPAN class=kwd>catch</SPAN> (Exception ex) { <SPAN class=kwd>throw</SPAN> ex; } } </pre>&nbsp;<font size="2"> <p>hope this makes sense! </p> </font> 2007-11-21T11:34:15-05:002060904http://forums.asp.net/p/1184845/2060904.aspx/1?Re+css+toolkit+adapters+apply+css+style+to+gridview+row+in+code+c+Re: css toolkit adapters- apply css style to gridview row in code (c#) <p>Nice.&nbsp; One suggestion would be to provide semantic names rather than visual names, for instance</p> <p><font><font size="2"><font color="#a31515" size="2">tr.AspNet-GridView-Error<br> tr.AspNet-GridView-Ok</font></font></font> </p> <p>This helps in the situation where someone decides they want non-error rows gray and error rows yellow.&nbsp; Then you don't end up with this in your CSS file:</p> <p><font><font size="2"><font color="#a31515" size="2">tr.AspNet-GridView-Red td: { color: yellow; }<br> tr.AspNet-GridView-Green td: { color: gray; }</font></font></font> </p> <p>Just a thought.&nbsp; I've seen this happen.&nbsp; :-)<br> </p> <p>&nbsp;</p> 2007-12-14T19:45:30-05:002074612http://forums.asp.net/p/1184845/2074612.aspx/1?Re+css+toolkit+adapters+apply+css+style+to+gridview+row+in+code+c+Re: css toolkit adapters- apply css style to gridview row in code (c#) <p>Yes good point. </p> <p>Colours should be&nbsp;assigned in the CSS not in the code. To&nbsp;further your&nbsp;point&nbsp;if another programmer creates another gridview&nbsp;he/she would have to assign the <font color="#a31515">AspNet-GridView-Red </font>class to colour the row yellow!</p> <p>What a mess!!</p> 2007-12-23T17:46:47-05:004846531http://forums.asp.net/p/1184845/4846531.aspx/1?Re+css+toolkit+adapters+apply+css+style+to+gridview+row+in+code+c+Re: css toolkit adapters- apply css style to gridview row in code (c#) <p>Error when attempting this:</p> <p>foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.GridView' because 'System.Web.UI.WebControls.GridView' does not have a public definition for 'GetEnumerator'</p> 2012-02-23T06:58:56-05:00