0
votes

We work with vb.net - Visual Studio 2013 (WinForms) We are changing datagrids to datagridviews in our code. At the moment I'm trying to convert a column that was described in the datagrid as follows:

Dim grdcolstyle5 As New PTSoft.FrameWork.DataBrowserTextColumnColorDecimal
With grdcolstyle5
.HeaderText = "text"
.MappingName = "vrd_199"
.Width = 80
.FormatString = "###,###,##0.00"
.[Operator] = "<"
.ParameterCol = 2
.ForeBrush = New SolidBrush(Color.Red)
End With

What is does is compare the value of one column with another and color the text when it's smaller (in this case). I know how to do it in a loop after the grid has been filled, but this slows things somewhat down. Does anyone know how this can be done "on the fly", on the moment the row is filled?

In short I am looking for the equivalent for the datagridview...

1

1 Answers

0
votes

You can use RowPrePaint event, where you have large number of rows to paint.

Private Sub dataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As DataGridViewRowPrePaintEventArgs)
If Convert.ToInt32(dataGridView1.Rows(e.RowIndex).Cells(2).Text) < Convert.ToInt32(dataGridView1.Rows(e.RowIndex).Cells(5).Text) Then
    dataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
End If
End Sub

The above code is comparing Cells(2) against Cells(5). You can customize that as per your need.

Another option is using CellFormattingEvent

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
    For Each Myrow As DataGridViewRow In dataGridView1.Rows
        If Convert.ToInt32(Myrow.Cells(2).Value) < Convert.ToInt32(Myrow.Cells(1).Value) Then
            Myrow.DefaultCellStyle.ForeColor = Color.Red
        End If
    Next
End Sub