0

Ive got a Datagrid populated from a datatable. I want to change the display font color for an entire row where the 5th column's (Column Name in SQL = DDLS_Num) value is greater than 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
4

2 回答 2

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
于 2013-06-21T16:04:22.570 回答
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
于 2013-06-20T18:22:27.520 回答