条件DataGrid格式化颜色

时间:2013-06-20 17:47:52

标签: sql vb.net datagridview

我已经从数据表中填充了一个Datagrid。我想更改第5列(SQL = DDLS_Num中的列名)值大于3的整行的显示字体颜色。

Using conn As SqlConnection = New SqlConnection(ConnectionString)
            conn.Open()
            Using comm As SqlCommand = New SqlCommand(sqlquery, conn)
                Dim rs As SqlDataReader = comm.ExecuteReader
                Dim dt As DataTable = New DataTable
                dt.Load(rs)
                datgDXLog.DataSource = dt
                ' If the 5th column's [or column name] value is >=3 Then
                ' DataGridView1.Rows[5] [OR COLUMN NAME?] .DefaultCellStyle.ForeColor = Color.Red
                ' EndIF

            End Using 'comm
        End Using 'conn

2 个答案:

答案 0 :(得分:2)

我认为你必须在RowPrepaint事件中这样做..

Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
    If DataGridView1.Rows(e.RowIndex).Cells(5).Value >= 3 Then
        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
    End If
End Sub

答案 1 :(得分:0)

处理DataGridView.RowDataBound事件中的着色:

Public Sub DataGridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles DataGridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        If IsNumeric(e.Row.Cells(4).Text) AndAlso CDbl(e.Row.Cells(4).Text) > 3 Then

            e.Row.ForeColor = System.Drawing.Color.Red

        End If 

    End If

End Sub