Wednesday, August 11, 2010

How to modify color of a datagrid?




So I am working with a datagrid and initially when I output something on it it shows correctly on the webpage. Then I click a button to get different data and the datagrid's background color goes white (data correctly changes). I change the data of the datagrid using javascript. Does someone know how I can make the datagrid alternate colors on each row like it does initially?





thanks


MikeHow to modify color of a datagrid?
This article describes another DataGrid control. Datagrids are very flexible, this one just inherits from the class DataGrid and adds the mouse over event to change the color of the row as the Code Project tables. Got the idea from Chris Maunder tables on Code Project. I looked at his JavaScript and I implemented into the .NET DataGrid class.





Using the code


This is a customer control. I include the source code for the control and a test project how to use it. As any customer control, you need to add the reference to the project. On the ASPX page:





Collapse Copy Code


(...)


%26lt; MOD:MODataGrid%26gt; id=';DataGrid1'; style=';Z-INDEX: 101;


LEFT: 320px; POSITION: absolute; TOP: 136px';


runat=';server';%26gt;


On the aspx.cs file add:





Collapse Copy Code


protected MouseOverDataGrid.MODataGrid DataGrid1;


I used the Datagrid tutorial example from http://asp.net for the test project





The Control


A simple Inherit from Datagrid class and overriding the Render function with Chris Maunder JavaScript. There are 2 public properties, the SpecialLinkColor as default set to CodeProject mouseover color that you can set to any color you like. And OverLinkHand to display the cursor as a Hand. I did not implement the method for on-click but it is on the JavaScript, so you can always implement it.





Collapse Copy Code


[assembly:TagPrefix(';MouseOverDataGrid';, ';MOD';)]


namespace MouseOverDataGrid


{


/// %26lt;summary%26gt;


/// Summary description for MouseOverDataGrid.


/// %26lt;/summary%26gt;


public class MODataGrid : System.Web.UI.WebControls.DataGrid


{





[Category(';Design';), Description(';OverLink Color';)]


private string sBackgroundColor = ';#dddddd';;


public string SpecialLinkColor


{


get{return(sBackgroundColor);}


set{sBackgroundColor=value;}


}





[Category(';Design';), Description(';OverLink Hand Type';)]


private bool bOverLinkHand = false;


public bool OverLinkHand


{


get{return(bOverLinkHand);}


set{bOverLinkHand=value;}


}





protected override void Render(System.Web.UI.HtmlTextWriter writer)


{


string sDataGridID = this.ID.ToString();





string sCursor = ';';;


if ( OverLinkHand == true )


sCursor = ';elm.style.cursor = Cursor;\r';;





string sJavaScript = ';%26lt; script language=';'\';';javascript\';'%26gt;\r'; +


';function ShowRow1(elm, BgColour, FgColour)\r'; +


';{\r'; +


';elm.bgColor = BgColour;\r'; + sCursor +


';}\r'; +


';function HilightRow(elm, hover, highlight)\r'; +


';{\r'; +


'; var BgColour = (hover)? \';'; + SpecialLinkColor + ';\'; : \';white\';;\r'; +


'; var FgColour = \';black\';;\r'; +


'; var Cursor = (hover)? \';hand\'; : \';auto\';;\r'; +


'; ShowRow1(elm, BgColour, FgColour);\r'; +


'; return false;\r'; +


';}\r'; +


';function RowOn() { return HilightRow(this, true, false); }\r'; +


';function RowOff() { return HilightRow(this, false, false); }\r'; +


';function RowClick()\r'; +


';{\r'; +


'; HilightRow(this, false, true);\r'; +


'; var elm = eval('document.getElementById(\';' + this.name + '_link\';)');\r'; +


'; if (elm) elm.click();\r'; +


'; return true;\r'; +


';}\r'; +


';var table = document.getElementById(\';'; + sDataGridID + ';\';);\r'; +


';var rows = table.getElementsByTagName(\';tr\';);\r'; +


';for (var I = 0; I %26lt; rows.length; i++) {\r'; +


';rows[i].onmouseover = RowOn;\r'; +


';rows[i].onmouseout = RowOff;\r'; +


//';rows[i].onclick = RowClick;'; +


';}\r%26lt;/script%26gt;\r';;











base.Render (writer);


base.Page.Response.Write(sJavaScript);


}

No comments:

Post a Comment