我在我的项目中使用vb.net。我有一个datagridview绑定到一个数据表m_table,它有一个名为Price_Change的列,带有十进制值。如果价格变化> 0,我想以绿色显示datagridview中的文本,否则显示红色。我不能简单地使用以下格式,因为有界数据表m_table是在我的代码中构造的,而不是直接通过数据库构建的。
DataGridView.Rows(0)Cells(0).Style.ForeColor=COLOR.BLACK
代码看起来像
Dim rowText As DataRow = m_table.NewRow
rowText("Price Change")=10.00 'assign values to price change column
' there is no color formating for data table
我想知道cellformatting事件是否可以用于此目的。它会减慢datagridview的负载吗?这是一个[链接] http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx
答案 0 :(得分:0)
Private Sub DGVTable_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVTable.CellFormatting
If Me.DGVTable.Columns(e.ColumnIndex).Name = columnPriceChange Then
If e.Value IsNot Nothing Then
Dim change As Decimal = CType(e.Value, Decimal)
If change >= 0 Then
e.CellStyle.ForeColor = Color.Green
Else
e.CellStyle.ForeColor = Color.Red
End If
End If
End If
End Sub