This is a migrated thread and some comments may be shown as answers.

Accessing Column Index

1 Answer 117 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott Manning
Top achievements
Rank 1
Scott Manning asked on 23 Nov 2014, 11:56 PM
I have a requirement to display data in a ASP.NET grid.  The columns are dynamically created as the table source is selected by the user and is not known at design time.  I am able to build the data table with the respective column names and display the data in the grid.  This is working OK, but I also have a requirement to color code specific columns.  The data source that I am display is the results on a data compare process so I have columns named MYFIELD1_S and MYFIELD1_T where the _S and _T represents the source and target.  I am need to compare the values in MYFIELD1_S versus MYFIELD1_T and if the values are different then color code MYFIELD1_T. 

I have some logic that is looking for the column name ending with "_T", get the column index and compare it to the column index-1 (i.e. the _S column).  But I have been having a hard time getting the column index.  I have tried the ItemCreated and ItemDataBound events without any luck and have looked at prerender.  I have this function working perfectly for a WinForm grid using CellFormatting event, but this does not exist in ASP.NET and we are moving from a WinForm app to ASP.NET

How is the best way to handle this requirement?

1 Answer, 1 is accepted

Sort by
0
Scott Manning
Top achievements
Rank 1
answered on 24 Nov 2014, 12:31 AM
After posting the question, I thought about looking at the prerender function and was able to get the solution to work nicely.  I am open to alternative ways if there is a better method.

foreach (GridDataItem item in RowGrid.Items)
{
for (int i = 0; i < item.Cells.Count; i++)
{
   GridTableCell celltgt = (GridTableCell)item.Cells[i];
   if (celltgt.Column.UniqueName.ToUpper().EndsWith("_T"))
   {
      GridTableCell cellsrc = (GridTableCell)item.Cells[i-1];
      if (celltgt.Text != cellsrc.Text)
      {
         cell.ForeColor = Color.DarkGreen;
      }
   }
}
}
Tags
Grid
Asked by
Scott Manning
Top achievements
Rank 1
Answers by
Scott Manning
Top achievements
Rank 1
Share this question
or