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 / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

format individual cells in DataGrid by data value and column type

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bruce - 05 Mar 2008 01:31 GMT
I have a web form with a programmatically generated DataGrid.  The DataGrid
columns are defined at run-time based on a SQL query.  A class is derived
from ITemplate to handle a fixed set of column types.  Here is a code
fragment:

namespace ApplianceBook
{
   public enum DisplayType
   {
       Fraction = 1,
       Percentage = 2,
       Integer = 3,
       Decimal = 4,
       String = 5
   }

   public class CTemplateColumn : ITemplate
   {
       private ListItemType columnListItemType;
       private string columnName;
       private int columnWidth;
       private DisplayType columnDisplayType;

       public CTemplateColumn(string ColumnName, int ColumnWidth,
ListItemType ColumnListItemType, DisplayType ColumnDisplayType)
       {
           columnName = ColumnName;
           columnWidth = ColumnWidth;
           columnListItemType = ColumnListItemType;
           columnDisplayType = ColumnDisplayType;
       }

       public void InstantiateIn(Control container)
       {
           switch (columnDisplayType)
           {
               case DisplayType.Fraction:
                   Label labelFraction = new Label();
                   labelFraction.ID = columnName;
                   labelFraction.Width = columnWidth;
                   if ((columnListItemType != ListItemType.Header) &&
(columnListItemType != ListItemType.Footer))
                   {
                       labelFraction.DataBinding += new
EventHandler(this.OnDataBindingFraction);
                   }
                   container.Controls.Add(labelFraction);
                   break;

               case DisplayType.Percentage:
                   Label labelPercentage = new Label();
                   labelPercentage.ID = columnName;
                   labelPercentage.Width = columnWidth;
                   if ((columnListItemType != ListItemType.Header) &&
(columnListItemType != ListItemType.Footer))
                   {
                       labelPercentage.DataBinding += new
EventHandler(this.OnDataBindingPercentage);
                   }
                   container.Controls.Add(labelPercentage);
                   break;

Everything works as expected and I am able to modify the actual text in each
cell based on its ColumnDisplayType.  However, I also want to be able to
format individual DataGrid cells based on ColumnDisplayType.  For example, a
cell with a ColumnDisplayType with percentage data should have its cell
background color set to red if the data value is greater than 50%.  Here is
my event handler for the Percentage column type:

public void OnDataBindingPercentage(object sender, EventArgs e)
{
   GridViewRow container;
   if (sender.GetType().ToString() == "System.Web.UI.WebControls.Label")
   {
       Label label = (Label)sender;
       container = (GridViewRow)label.NamingContainer;
       // Format data to display as a percentage value.
       label.Text = String.Format("{0:0%}",
((DataRowView)container.DataItem)[columnName]);
   }
}

In the above code I am able to access the data stored in the cell via
((DataRowView)container.DataItem)[columnName] but I can't find a way to
access the cell object containing the data so that I can apply cell-level
formatting.  The container object doesn't seem to support accessing a
particular cell in the row using the the columnName as an index.

I want to be able to do something like the following (fictitious) code:

if (((DataRowView)container.DataItem)[columnName] > .5)
{
   ((DataRowView)container.DataItem).Row.Cells[columnName].BackgroundColor
= Red;
}

Unfortunately, DataRowView.Row does not have a Cells property.

How do you access the DataGrid cell associated with the current databinding
event?  Any instructions or sample code for doing so would be a big help.  
Thanks.
Bruce - 05 Mar 2008 21:32 GMT
OK, I found a way to do what I want but it sure is ugly.  There's got to be a
better way than iterating through all cells to find the one with the matching
ID:

public void OnDataBindingPercentage(object sender, EventArgs e)
{
   GridViewRow container;
   if (sender.GetType().ToString() == "System.Web.UI.WebControls.Label")
   {
       Label label = (Label)sender;
       container = (GridViewRow)label.NamingContainer;
       // Format data to display as a percentage value.
       label.Text = String.Format("{0:0%}",
((DataRowView)container.DataItem)[columnName]);
       // Find index of cell with matching ID.
       int CellIndex = -1;
       for (int i = 0; i < container.Cells.Count; i++)
       {
           if (container.Cells[i].Controls[0].ID == columnName)
           {
               CellIndex = i;
               break;
           }
       }
       if (CellIndex != -1)
       { // Perform cell-level formatting.
           ((GridViewRow)container).Cells[CellIndex].HorizontalAlign =
HorizontalAlign.Center;
       }
       break;
   }
}

> I have a web form with a programmatically generated DataGrid.  The DataGrid
> columns are defined at run-time based on a SQL query.  A class is derived
[quoted text clipped - 97 lines]
> event?  Any instructions or sample code for doing so would be a big help.  
> Thanks.

Rate this thread:







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.