Question

In our application we have style sheets to define common colors etc… I wrote a quick and dirty function where I get a dataset from a stored procedure, lop off the columns that I don’t want to show, cram it into a programmatically generated DataGrid, set that DataGrid’s styles, then export it to Excel. Everyone loves the colors in the Excel output (Gasp! They match the DataGrid colors, blah blah blah…).

My final piece I would like to add to it is that I would like to programmatically access a style and grab color codes and other items from it (.IntranetGridHead) instead of hard coding them, which is what I am doing now.

int iHeaderColor = Convert.ToInt32 ("D0D7E8", 16);
DataGrid dg = new DataGrid();
dg.DataSource = dsReturnDataSet.Tables[0].DefaultView;
dg.DataBind();

dg.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(iHeaderColor);
dg.HeaderStyle.Font.Bold = true;
dg.HeaderStyle.Font.Size = 10;

Obviously then whenever the company goes through another “rebranding” and the style sheet values change, the excel colors will automatically match and I will get a big (pat on the back||cookie).

Any thoughts from the C# people who know more than I (which would be most of you…)?
Thanks,
Michael

Was it helpful?

Solution

All Web.UI.Control objects have a .Styles property, which can be accessed as Styles["Name"]. Therefore you can do this:

DataTable dt = LookupStyles();
dg.Styles.Clear();
foreach (DataRow dr in dt.Rows)
  dg.Styles.Add(dr["StyleName"].ToString(), dr["StyleValue"].ToString());

I had a similar thought several months ago :) Note for this to work right, your grid must be runat="server".

Edit: Looks like you want to READ the grid and use that... If you're using a .CssStyle and a Stylesheet (.css), you'll have to do an HTTP GET to that css file and parse it yourself.

OTHER TIPS

You would have to parse the CSS file yourself, and IIRC there is no CSS file parser in the .NET framework out of the box. However, there is a free one you can find here:

http://www.codeproject.com/KB/recipes/CSSParser.aspx

You should then be able to use this to parse the CSS file. From there you should be able to get the style and the value and apply it to your data grid.

I wrote something along these lines a while back. It involved a HttpHandler to deal with the CSS files, a change to the IIS config to get asp.net to receive the requests for the CSS files, and a simple xml file structure which contained my colour definitions. But you've done that with a database, which is fine too.

Then in the CSS I had something like this ...

.button
{
    background-color: $colours:button-background-colour;
    color: $colours:button-text-colour;
}

with my xml defining the values for button-background-colour and button-text-colour. I used regular expression text replacement to process the CSS file substituting in the relevant values from the xml file.

I'm sure you could take some of those ideas and combine it with your existing code to get the desired effect. You will of course need to deal with caching and changes to your database/xml file.

Hope that helps.

If you need any pointers on any of that then I'm sure I can dig out some example code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top