Programmatically formatting individual cells in datagrid

J

jacqueharper

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 ==
ListItemType.AlternatingItem)
{
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
e.Item.Cells[*columnNameFromDataTable*].
}
}
}

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

Thanks for any help!
Jacque
 
C

ChevolDavis

Couldn't you use something like this

int n = 2
string cell = 1

//n is the item index in the datagri

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

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

//this would change the background color or the data in row 2 colum
1
 
J

jacqueharper

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!

Jacque

public void OnDataBindValuesCheck (object sender, DataGridItemEventArgs
e)
{
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;
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top