社区
C#
帖子详情
如何设置datagrid 单元格颜色?
bennylee0618
2005-08-11 10:38:07
datagrid中有如下数据
姓名 性别 年龄
S 男 20
A 女 22
B 女 30
要求将年龄大于等30的格子以黄色背景显示,性别为男的数据行以蓝色字体显示,请问应该怎么做啊?
谢谢了
...全文
417
7
打赏
收藏
如何设置datagrid 单元格颜色?
datagrid中有如下数据 姓名 性别 年龄 S 男 20 A 女 22 B 女 30 要求将年龄大于等30的格子以黄色背景显示,性别为男的数据行以蓝色字体显示,请问应该怎么做啊? 谢谢了
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用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
WPF
DataGrid
如何定义每个
单元格
不同
颜色
根据条件改变
颜色
动态
设置
每个元素
颜色
WPF中
datagrid
动态
设置
单元格
背景WPF
DataGrid
第一列与第二列元素比大小后,显示不同的
颜色
对于涉及比较或计算的业务场景,可以通过自定义数据绑定和转换器来反映数据间的逻辑关系,如第一列与第二列元素比大小后显示不同
颜色
,使得复杂的业务规则得以清晰、直观地呈现在用户面前。如何实现,之前已经有介绍触发器、转换器等方法,现在介绍另一种方法,业务逻辑放在绑定数据后面处理,多拿一个字段来显示处理好的
颜色
就行,以下有完整例子。通过数据绑定实现
DataGrid
中
单元格
颜色
的动态设定,不仅能够丰富用户界面的表现力,还能有效结合业务逻辑,提升应用的整体性能与用户体验。
Flex
设置
DataGrid
单元格
颜色
以及表头背景
颜色
渐变
1、
设置
DataGrid
单元格
颜色
package com.tjsoft.base.common.page { import flash.display.Sprite; import mx.controls.
DataGrid
; public class Colors
DataGrid
extends
DataGrid
{ public function Colors
DataGrid
(
将
DataGrid
特定列
单元格
的背景
颜色
在后台
设置
本例是将DataTable中的数据显示到
DataGrid
中。用List绑定的原理差不多。 XAML <
DataGrid
TemplateColumn Width="80" Header="
颜色
"> <
DataGrid
TemplateColumn.CellTemplate> <Dat...
MahApps用户UI库中
DataGrid
中
单元格
字体
颜色
根据数字动态变化的样式
设置
为了改变
DataGrid
中
单元格
字体
颜色
,可以
设置
CellStyle属性。 首先定义Style,使其从MahApps.Styles.
DataGrid
Cell继承,然后将定义好的style赋值为
DataGrid
TextColumn的CellStye属性,可以从xaml中赋值,也可以在back code中赋值。 以下两个例子: (1)绑定到枚举值的例子,枚举直接写出其整数值,不要直接写枚举的字符 <Style x:Key="TextBlockForMyEnumCellSt
datagrid
设置
单元格
边框,如何更改
DataGrid
View中某些
单元格
的边框
颜色
?
I need to programming change the border color of some cells in the CellFormatting event. Can the board color of an individual cell be changed?解决方案You can draw a rectangle. In this example I put a red ...
C#
111,128
社区成员
642,534
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章