Welcome Guest Search | Active Topics | Sign In | Register

Setting the background color of a Grid Cell Options
Chris
Posted: Wednesday, June 17, 2009 12:05:01 PM
Rank: Member
Groups: Member

Joined: 4/29/2009
Posts: 13
Hi,

I defined a grid during design time with 0 columns. During runtime, I create the grid columns (7) dynamically. This works great! Now, based on the data table that this grid is bound to, one of the columns is color. Instead of showing the value of the color in the "Color" column, I want to set the background of the cell to the color corresponding to the color value in that cell.

For instance, Color column is column index 1. The cell value for Col[1]Row[1] is 255. Instead of show "255", I want the background of that cell to be "red"

I figured that I I have to do this in the pre-render event...

Your help is greatly appreciated

Thanks
eo_support
Posted: Wednesday, June 17, 2009 12:47:52 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

You will need to set the columns' DataFormat property so that it instead of talking the number as is, it generates a small HTML segment to fill the Grid cell. For example, assuming that your database has red value in hex format, you can set the column's DataFormat to something like this:

Code: HTML/ASPX
<div style="background-color:#{0}0000;width:10px;height:10px;"></div>


This way if your database has value "ff", then the final HTML that is used to fill the grid cell will be

Code: HTML/ASPX
<div style="background-color:#ff0000;width:10px;height:10px;"></div>


Which will renders as a 10 by 10 red block. Note that it replaces {0} with your data. Obviously without DataFormat, it will render it directly as "ff".

Note you can only use one value with DataFormat and in order to use it as color, it must be hex format. So if you need to take all three colors, the value you pass to the Grid must be a 6 digits hex number. For example, FFFF00 for yellow. This means if the value is stored differently, you will need to format it into a 6 digits string. You can perform such conversion at:

1. In your SQL. For example, you can create a user function to perform such conversion and then call this function in your SELECT statement. I am not sure whether your database server has such function built-in. I do not think SQL Server has it. So if you use SQL Server, you will need to create one. There are such code online though;

2. Perform the conversion on your data source object with C#/VB code --- to load the data from your database to an ADO.NET object such as DataTable, then modify that DataTable and format the color field, then pass the DataTable object to the Grid;

3. Perform the conversion on the Grid. You can populate whatever in your database into the Grid first, then loop through all grid items and modify the color cell's Value property;

Obviously if you already store the color hex code in your database, then no conversion is needed at all.

Thanks!

You
Chris
Posted: Wednesday, June 17, 2009 1:25:38 PM
Rank: Member
Groups: Member

Joined: 4/29/2009
Posts: 13
Thanks for your quick response.

Although I get the gist of the solution, the part about setting the DataFormat such that it "generates a small HTML segment" escapes me.

Do you mean in the code where I create the column, I do:

EO.Web.StaticColumn sc = new StaticColumn();
sc.HeaderText = column.ColumnName;
sc.DataField = column.ColumnName;
sc.DataFormat = "<div style="background-color:#{0}0000;width:10px;height:10px;"></div>";

Thanks,
Chris



eo_support
Posted: Wednesday, June 17, 2009 1:32:17 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Yes. The key is that the Grid takes whatever value in your database, then call string.Format with the DataFormat you provided to format it. It then displays the formatted result. Here is another example of a typical usage of DataFormat: Assuming that you have image Urls (for example, "/images/abc.gif") in your database and you want to display the actual image in the Grid. If you do not set DataFormat, you will see the image Url in your Grid (for example, literally "/images/abc.gif"). However you can set DataFormat to something like this to display the actual image:

Code: HTML/ASPX
<img src="{0}" />


It will be easier for you to see how it works if you try the feature out with a static Grid. DataFormat works the same way regardless whether the column is dynamically created or not.

Thanks!
Chris
Posted: Wednesday, June 17, 2009 2:12:50 PM
Rank: Member
Groups: Member

Joined: 4/29/2009
Posts: 13
Thank you so much - it works fantastic!

One other question, can I use the same technique to align the text in the columns?
eo_support
Posted: Wednesday, June 17, 2009 2:23:21 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Yes. You can use it for any purpose that can be achieved with simple HTML. It won't be a good solution for you to align text though --- the HTML element you put in (in your case the div) won't resize dynamically along with the column. So if you want to align to the right and also allow column resizing. You got to use something else ---- usually by including "text-align:right" on the CellStyle property for that column. For example:

Code: C#
sc.CellStyle.CssText = "text-align:right";


Thanks
Chris
Posted: Wednesday, June 17, 2009 2:26:23 PM
Rank: Member
Groups: Member

Joined: 4/29/2009
Posts: 13
Perfect - thanks for you excellent support


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.