471,350 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Programmatically formatting individual cells in datagrid

I need to change the formatting of individual cells in a datagrid. I
cannot figure out how to address individual cells by their column name.

I have a DataTable which contains (for instance) columns called Weight,
Length and Diameter.
The table is bound to a DataGrid, and I am using the ItemDataBound
behavior to execute code that checks values in the table, e.g.:

public void OnDataBindValuesCheck (object sender,
DataGridItemEventArgs e)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
double testLength =
Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "Length"));
if (testLength > 30)
// highlight the cell which contains the value that is
greater then 30.
// e.Item.BackColor will set the background color for the
whole row.
// e.Item.Cells[*int*].BackColor will set the color _if_
I know
// the ordinal number of the cell, but I can't count
on that.
// What I need is something like

(see the "comments" in my psuedocode for the dilemna)

Thanks for any help!

Dec 20 '05 #1
2 7194
Couldn't you use something like this:

int n = 2;
string cell = 1;

//n is the item index in the datagrid

//cell is the cell number of the data you want

DataGrid1.Items[n].Cells[cell].BackColor = "some color";

//this would change the background color or the data in row 2 column

ASP.NET Developer Community
Dec 23 '05 #2
Thanks, ChevolDavis.

I don't want to be constrained by having to know the cell number. That
is, I want to leave freedom in the design to rearrange the columns of
the datagrid, without having to change the code.

I am having some success with the code below, making use of the
boundField property of the DataGridColumnCollection and a hash table to
act as an index. If anyone has further suggestions I would still like
to hear them!


public void OnDataBindValuesCheck (object sender, DataGridItemEventArgs
TableCell objectCell = new TableCell();
int cell_index;

// Cast the sender as a DataGrid object.
DataGrid replicadgBedSize = new DataGrid();
replicadgBedSize = ((DataGrid)sender);
// Not quite sure why, but an ArrayList is
// needed to create the DataGridColumnCollection.
ArrayList alC = new ArrayList();
// Get the DataGridcolumnCollection
// object from the sender DataGrid
DataGridColumnCollection dgcc = new
DataGridColumnCollection(((DataGrid)sender), alC);
dgcc = replicadgBedSize.Columns;
// Create a hash table to use as an index to the columns
Hashtable cell_datafield = new Hashtable();
int count = 0;
foreach (BoundColumn c in dgcc)
cell_datafield[c.DataField.ToString()] = count;

// Now the hashtable can be
// used to look up the column
// name and return it's location
// in the data grid
if (/*data in a particular column "ColumnName" meets a condition*/)
cell_index = Convert.ToInt32(cell_datafield["ColumnName"]);
objectCell = e.Item.Cells[cell_index];
objectCell.BackColor = System.Drawing.Color.Red;

Dec 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Matthew Wieder | last post: by
1 post views Thread by euan | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.