Try this. It's simpler.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<dataStruct> mylist = new List<dataStruct>();
mylist.Add(new dataStruct(new DateTime(1982, 01, 01), "Bill", 'm'));
mylist.Add(new dataStruct(new DateTime(1981, 01, 01), "Philip", 'm'));
mylist.Add(new dataStruct(new DateTime(1980, 01, 01), "Susan", 'f'));
GridView1.DataSource = mylist;
GridView1.DataBind();
}
public struct dataStruct
{
private DateTime birthdate;
private string name;
private char sex;
public DateTime BirthDate
{
get { return birthdate; }
set { birthdate = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public char Sex
{
get { return sex; }
set { sex = value; }
}
public dataStruct(DateTime _birthdate, string _name, char _sex)
{
this.birthdate = _birthdate;
this.name = _name;
this.sex = _sex;
}
}
private List<int> changeColorOnThisColumn;
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// initialize changeColorOnThisColumn if it has not been initialized yet.
if (changeColorOnThisColumn == null)
changeColorOnThisColumn = new List<int>();
// If this is the header row
if (e.Row.RowIndex == -1)
{
// initialize a counting variable to keep track of the cell index
int index = 0;
foreach (TableCell cell in e.Row.Cells)
{
// if the Column is the column that you want to change the color for, add it to the list of columns
if (cell.Text == "BirthDate")
changeColorOnThisColumn.Add(index);
index++;
}
}
// If this is not the header row
else if (e.Row.RowIndex != -1)
{
dataStruct curStruct = (dataStruct)e.Row.DataItem;
if (curStruct.Sex == 'f')
e.Row.Cells[0].BackColor = Color.Pink;
else
e.Row.Cells[0].BackColor = Color.Blue;
foreach (int index in changeColorOnThisColumn)
{
e.Row.Cells[index].BackColor = Color.Beige;
}
}
}
}
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105:





by: Infinite_RecursionPosted on 2007-12-10 at 14:16:18ID: 20445793
Tc.Style.Add(HtmlTextWrite rStyle.Bac kgroundCol or, "Red");