设置datagridview背景色?

appleller 2009-06-13 11:21:34
怎样根据datagridview单元格的值来设置datagridview背景色?
例如数值为负数的单元格背景色为红色
...全文
655 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanjs 2009-06-14
  • 打赏
  • 举报
回复
学习
outou 2009-06-14
  • 打赏
  • 举报
回复
代码来自http://bingning.net/free/source/datagridview/alternatingrows.html

'CellFormatting事件处理器
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)

'确认单元格的列
If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
TypeOf e.Value Is Integer Then
Dim val As Integer = CInt(e.Value)
'根据单元格的值,变更背景色
If val < 0 Then
e.CellStyle.BackColor = Color.Red
Else
e.CellStyle.BackColor = Color.Blue
End If
End If
End Sub
appleller 2009-06-14
  • 打赏
  • 举报
回复
是根据具体的单元值来设置当前格的背景颜色
datagridview
例如: 某一列,数值为正单元格的背景色设为蓝色
数值为负单元格的背景色设为红色

C/S结构,能否具体代码? 谢谢!
wuyq11 2009-06-14
  • 打赏
  • 举报
回复
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.RowIndex != -1 && e.ColumnIndex == 1 && e.Value != null && e.Value.ToString() == "")
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.Blue;
e.CellStyle.SelectionBackColor = Color.BlueViolet;
}
}
按照流导出
或导出excel设置单元格格式
Microsoft.Office.Interop.Excel.Range myRange22 = myWorkSheet.get_Range("B2","B2");
myRange22.Font.Bold = true;
myRange22.Font.Size ="20";
参考



zengfanying 2009-06-14
  • 打赏
  • 举报
回复
学习
ljhcy99 2009-06-14
  • 打赏
  • 举报
回复
DataGridView1_CellFormatting 事件里写啊
appleller 2009-06-14
  • 打赏
  • 举报
回复
OK!

DataGridViewpus.Item(i, j).Style.BackColor = Color.Red

还有个问题,输出EXCEL的时候背景色没有了,怎样设置才能把背景色也一起输出呢?
wjq 2009-06-13
  • 打赏
  • 举报
回复
C/S还是B/S ?
C/S的话,重画一些单元格
B/S的话,ItemBind事件里给Cell加上Style

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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