datagrid单元格颜色设置问题

TQSHHHH 2004-08-17 09:33:42
各位高手请问:怎么改变datagrid中某个单元格的背景色
...全文
457 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
TQSHHHH 2004-08-23
  • 打赏
  • 举报
回复
这只是对行号和列号的判断,我想对两个单元格的值进行比较,该怎么做
dofly 2004-08-21
  • 打赏
  • 举报
回复
Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms


Public Class Form1
Inherits Form
Private WithEvents dataGrid1 As DataGrid
Private components As Container


Public Sub New()
MyBase.New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
'
' TODO: Add any constructor code after InitializeComponent call
'

End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then
If (Not (components) Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)

End Sub

Private Sub InitializeComponent()

Me.dataGrid1 = New System.Windows.Forms.DataGrid()
CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
' dataGrid1
'
Me.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.dataGrid1.DataMember = ""
Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dataGrid1.Location = New System.Drawing.Point(40, 24)
Me.dataGrid1.Name = "dataGrid1"
Me.dataGrid1.Size = New System.Drawing.Size(576, 208)
Me.dataGrid1.TabIndex = 0
'
' Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(648, 253)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dataGrid1})
Me.Name = "Form1"
Me.Text = "Form1"
AddHandler Me.Load, New System.EventHandler(AddressOf Form1_Load)
CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

<STAThread()> _
Public Shared Sub Main()

Application.Run(New Form1())

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

Me.dataGrid1.DataSource = SomeDataTable()
AddCellFormattingColumnStyles(Me.dataGrid1, New FormatCellEventHandler(AddressOf FormatGridCells))
'add an event handler to select whole rows only
'uncomment this if you want this behavior
'this.dataGrid1.CurrentCellChanged += new EventHandler(SelectWholeRow);

End Sub

Private Sub FormatGridCells(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)

'color row 1 red
'color column 4 blue
If (e.Row = 1) Then
e.BackBrush = Brushes.Red
End If
'set font of some cells to bold
If (e.Column = 4) Then
e.BackBrush = Brushes.Blue
End If
'set textcolor of some cells to blue
If (((e.Row + e.Column) Mod 5) = 0) Then
e.TextFont = New Font(e.TextFont.Name, e.TextFont.Size, FontStyle.Bold)
End If
'set font of some cells to bold, underline, italic with white text on green background
If (((e.Row + e.Column) Mod 8) = 0) Then
e.ForeBrush = Brushes.DodgerBlue
End If
If (((e.Row + e.Column) Mod 9) = 0) Then
e.TextFont = New Font(e.TextFont.Name, e.TextFont.Size, ((FontStyle.Bold Or FontStyle.Italic) _
Or FontStyle.Underline))
e.ForeBrush = Brushes.White
e.BackBrush = Brushes.Green
End If

End Sub
Private Sub SelectWholeRow(ByVal sender As Object, ByVal e As EventArgs)

If (Me.dataGrid1.CurrentRowIndex > -(1)) Then
Me.dataGrid1.Select(Me.dataGrid1.CurrentRowIndex)
End If

End Sub
Private Function SomeDataTable() As DataTable

Dim dt As DataTable
dt = New DataTable("MyTable")
'add some columns
Dim nCols As Integer
nCols = 10
Dim j As Integer
j = 0

Do While (j < nCols)
dt.Columns.Add(New DataColumn(System.String.Format("col{0}", j), GetType(System.String)))
j = (j + 1)

Loop
'add some rows
Dim nRows As Integer
nRows = 40
Dim i As Integer
i = 0

Do While (i < nRows)
Dim dr As DataRow
dr = dt.NewRow
Dim jj As Integer
jj = 0

Do While (jj < nCols)
dr(jj) = System.String.Format("row {0} col {1}", i, jj)
jj = (jj + 1)

Loop
dt.Rows.Add(dr)
i = (i + 1)

Loop
dt.DefaultView.AllowNew = False
'turn off append row
Return dt

End Function
Private Sub AddCellFormattingColumnStyles(ByVal grid As DataGrid, ByVal handler As FormatCellEventHandler)

Dim ts As DataGridTableStyle
ts = New DataGridTableStyle()
Dim dt As DataTable
dt = CType(grid.DataSource, DataTable)
ts.MappingName = dt.TableName
Dim j As Integer
j = 0

Do While (j < dt.Columns.Count)
Dim cs As DataGridFormattableTextBoxColumn
cs = New DataGridFormattableTextBoxColumn(j)
cs.MappingName = dt.Columns(j).ColumnName
cs.HeaderText = dt.Columns(j).ColumnName
AddHandler cs.SetCellFormat, handler
ts.GridColumnStyles.Add(cs)
j = (j + 1)

Loop
grid.TableStyles.Clear()
grid.TableStyles.Add(ts)

End Sub
End Class

dofly 2004-08-21
  • 打赏
  • 举报
回复
http://www.syncfusion.com/faq/winforms/Files/DataGridCellFormatting_vb.zip

这个列子上面有办法
yingshis 2004-08-21
  • 打赏
  • 举报
回复
up
拼命三朗 2004-08-20
  • 打赏
  • 举报
回复
关注
TQSHHHH 2004-08-20
  • 打赏
  • 举报
回复
不好意思,我还需要再问一下,zoubf,你给我的例子能否实现两个列的值进行比较,然后变色
冰峰zoubf 2004-08-18
  • 打赏
  • 举报
回复
下载这个看看吧
http://www.syncfusion.com/faq/winforms/Files/DataGridCellFormatting_vb.zip
yubing9 2004-08-17
  • 打赏
  • 举报
回复
http://vb.studysea.net/BBS/DispBBS.jsp?BoardID=0&BMID=108&TopicID=1632
看看这个,对你有没有帮助
AntingZ 2004-08-17
  • 打赏
  • 举报
回复
http://dev.csdn.net/develop/article/15/15928.shtm
outspaceman 2004-08-17
  • 打赏
  • 举报
回复
:;;;;;;;;;;;;;;

16,552

社区成员

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

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