C# - Winform - DevExpress - GridControl 任意条件控制Row背景色。



 1 private void gvSendConfirm_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
 2         {
 3             string sCurrTime=DbHelperSql.GetSingle("Select GetDate() DateNow").ToString();
 4             DateTime dtCurrTime,dtSendTime;
 5             int iMinutes = 0;
 6             try
 7             {
 8                 dtCurrTime=DateTime.Parse(sCurrTime);
 9             }
10             catch (System.Exception ex)
11             {
12             dtCurrTime=DateTime.Now;
13             }
14 
15             string sSendTime= gvSendConfirm.GetDataRow(e.RowHandle)["UpdateTime"].ToString();
16             try
17             {
18                 dtSendTime = DateTime.Parse(sSendTime);
19             }
20             catch (System.Exception ex)
21             {
22                 dtSendTime = DateTime.Now;
23             }
24 
25             //计算时间差。
26             System.TimeSpan difTime = dtCurrTime.Subtract(dtSendTime);
27             iMinutes = difTime.Minutes;
28 
29             if (iMinutes <= iTimeOutLevel1)
30             {
31                 e.Appearance.BackColor = Color.White;   //设置背景色
32             }
33             else if (iMinutes > iTimeOutLevel1 && iMinutes <= iTimeOutLevel2)
34             {
35                 e.Appearance.BackColor = Color.FromArgb(192, 192, 255);//设置背景色
36             }
37             else if (iMinutes > iTimeOutLevel2 && iMinutes <= iTimeOutLevel3)
38             {
39                 e.Appearance.BackColor = Color.FromArgb(128, 255, 128);//设置背景色
40             }
41             else if (iMinutes > iTimeOutLevel3 && iMinutes <= iTimeOutLevel4)
42             {
43                 e.Appearance.BackColor = Color.FromArgb(255, 192, 128);//设置背景色
44             }
45             else if (iMinutes > iTimeOutLevel4 && iMinutes <= iTimeOutLevel5)
46             {
47                 e.Appearance.BackColor = Color.FromArgb(255, 128, 128);//设置背景色
48             }
49             else if (iMinutes > iTimeOutLevel5 && iMinutes <= iTimeOutLevel6)
50             {
51                 e.Appearance.BackColor = Color.Red;//设置背景色
52             }
53             else if (iMinutes > iTimeOutLevel6)
54             {
55                 e.Appearance.BackColor = Color.Crimson;//设置背景色
56             }
57             e.Appearance.ForeColor = Color.Black; //设置字体颜色
58         }

 

转载于:https://www.cnblogs.com/kliine/p/9246736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm中,可以通过自定义单元格样式来实现GridControl中单元格文字的不同颜色或背景色。具体步骤如下: 1. 在GridControl的Properties窗口中找到Appearance属性,将其设为DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None,这样可以去掉单元格的焦点框。 2. 在GridControl上双击打开GridView的Designer界面。 3. 选中要修改样式的列,找到AppearanceCell属性,单击右侧的“...”按钮打开AppearanceObject对象的编辑器。 4. 在AppearanceObject编辑器中,可以设置字体、背景色、前景色等属性,还可以通过条件格式化来实现更复杂的样式。例如,可以在FormatConditions列表中添加条件格式化规则,然后在Appearance属性中设置对应的样式。 5. 保存样式后,关闭Designer界面即可看到效果。 下面是一段示例代码,演示如何在GridView中实现单元格文字颜色的动态变化: ```csharp private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { if (e.Column.FieldName == "Status") { string status = e.CellValue.ToString(); if (status == "OK") { e.Appearance.ForeColor = Color.Green; } else if (status == "Warning") { e.Appearance.ForeColor = Color.Yellow; } else if (status == "Error") { e.Appearance.ForeColor = Color.Red; } } } ``` 这段代码在GridViewCustomDrawCell事件中,判断当前单元格的列名是否为“Status”,如果是,则根据单元格的值来设置不同的字体颜色。可以根据自己的需求修改相应的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值