如何设置datagrid 单元格颜色?

bennylee0618 2005-08-11 10:38:07
datagrid中有如下数据
姓名 性别 年龄
S 男 20
A 女 22
B 女 30

要求将年龄大于等30的格子以黄色背景显示,性别为男的数据行以蓝色字体显示,请问应该怎么做啊?
谢谢了
...全文
360 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdo 2005-08-11
  • 打赏
  • 举报
回复
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
KingSeaMountain 2005-08-11
  • 打赏
  • 举报
回复
建议使用第三方控件,绝对划算
cuike519 2005-08-11
  • 打赏
  • 举报
回复
//在Item_DataBind里面判断并实现即可。
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
if(e.Item.Cells[1].Text == "男")
e.Item.ForeColor = Color.FromName("blue");
int xxx = Int32.Parse(e.Item.Cells[2].Text==""||e.Item.Cells[2]==null?"0":e.Item.Cells[2].Text);
if(xxx > 30)
e.Item.BackColor = Color.FromName("yellow");
}
}
bennylee0618 2005-08-11
  • 打赏
  • 举报
回复
注:是winForm,不是webForm,好像不能用ItemDataBound事件
ufrshchenw 2005-08-11
  • 打赏
  • 举报
回复
这边有个例子你可以自己看看怎么实现的
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
stonegoldaustin 2005-08-11
  • 打赏
  • 举报
回复
//为事件建立委托.
public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);

public class DataGridCellColorTextBoxColumn : DataGridTextBoxColumn
{
public event CellColorEventHandler CheckCellColor;

public DataGridCellColorTextBoxColumn()
{
}

//继承DataGridTextBoxColumn的Pain事件.
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
if(CheckCellColor != null)
{
//重绘画时,设置当前行的背景色
DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, Color.White);
CheckCellColor(this, e);

if(e.BackColor != Color.White)
backBrush = new SolidBrush(e.BackColor);
}

base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
}

int numCols = cls_SQL.m_dst.Tables["m_CurrentStorage"].Columns.Count;
DataGridCellColorTextBoxColumn columnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
columnTextColumn = new DataGridCellColorTextBoxColumn();
columnTextColumn.HeaderText = cls_SQL.m_dst.Tables["m_CurrentStorage"].Columns[i].ColumnName;
columnTextColumn.MappingName = cls_SQL.m_dst.Tables["m_CurrentStorage"].Columns[i].ColumnName;

//为每个单元格建立设置背景色的事件.
columnTextColumn.CheckCellColor += new CellColorEventHandler(SetColorValues);

tableStyle.GridColumnStyles.Add(columnTextColumn);
}


//新增改变颜色
public void SetColorValues(object sender, DataGridCellColorEventArgs e)
{
//根据条件, 将相关行设置不同的背景色.
if(Convert.ToInt32(dg_CurrentStorage[e.Row,7]) <= 10)
e.BackColor = Color.Red;
else
e.BackColor = Color.White;
}
TechEye 2005-08-11
  • 打赏
  • 举报
回复
用UltraGrid

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧