Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Programmatically formatting individual cells in datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jacqueharper@earthlink.net - 20 Dec 2005 20:39 GMT
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
ChevolDavis - 23 Dec 2005 09:56 GMT
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

---------------------------------
http://community.ihostasp.ne
ASP.NET Developer Community
jacqueharper@earthlink.net - 27 Dec 2005 15:01 GMT
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;
 }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.