473,467 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 ==
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

Dec 20 '05 #1
2 7302
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
1.

----------------------------------
http://community.ihostasp.net
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!

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;
}
}

Dec 27 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob Rosen | last post by:
The description given in "Customizing Items Dynamically in the DataList or DataGrid Web Server Control" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbCode/...
2
by: Richard | last post by:
I know that you can format a grid and its columns using DataGridTableStyle and DataGridTextBoxColumn in VB.NET. But can you change the format of the individual rows, or even cells, to something...
1
by: David | last post by:
In the event handler for ItemCommand, I am trying to read text of cells in the row programmatically, but can't get it working. //this is what I have in InitializeComponent...
7
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is...
18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
1
by: strout | last post by:
I know it's dumb ... sometimes frustrating I want the text and a dropdownlist align with both side of a table cell, i.e. text align left and dropdownlist align right. I cannot add more cells to...
0
by: d pak | last post by:
Here is a snippit which replicates my issue. I have a datagrid which contains an input textbox on each row, binded on the serverside. However it seems that when I perform a postback to refresh teh...
1
by: euan | last post by:
HI Guys, I have bee using conditional formatting in the datagrid recently and I am moving over to framework 2.0 and noticed the datagrid has been replaced by the gridview. So, I would like to do...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.