none
datagridview : alternative row color : base on a condition

    Question

  • if there a way to change the row back color by a column value?

    Example

    so_num, so_line

    2525,1

    2525,2

    2526,1

    2526,2

    2527,3

    2527,4

    to (I am changing the row back color of all rows that have the same so_num (Sales Order Number)

    so_num, so_line

    2525,1, white

    2525,2, white

    2526,1, pink

    2526,2,pink

    2527,3, white

    2527,4, white

    thank you all

    John


    • Edited by Bre-x Monday, March 20, 2017 6:12 PM
    Monday, March 20, 2017 6:11 PM

All replies

  • The answer can be yes,. 

    However, with complex controls of the DataGridView it depend always

    1. How you use it
    2. How you have filled it
    3. How good your program knowledge is

    Here a sample on our website how you can change the backcolor of a row in a condition.

    http://www.vb-tips.com/DGVRowColor.aspx


    Success
    Cor

    Monday, March 20, 2017 6:18 PM
  • Thank you for your reply

    On your example it is either M or F

    If drv.Item("Gender").ToString = "M" Then

    but in my case I can never know the sales order number ahead.

    It would be a combination of current_so and next_so

    if so_num = current_so then

    pink

    else

    white

    end if

    But how the current_so becomes the next_so is my problem

    and I can not figure it out.

    John


    • Edited by Bre-x Monday, March 20, 2017 7:16 PM
    Monday, March 20, 2017 6:32 PM
  • It is a datagridview, not a spreadsheet, don't expect that it can act like that. 

    A datagridview is made to show and enter data row by row. 

    The little acknowledge button in the row header makes that it is pushed down the source. 

    Be aware that nevertheless there are 3rd party datagrids, made to fulfil all kind of wishes and probably also yours.

    https://www.google.com/?hl=en&gws_rd=cr#hl=en&q=3rd+party+datagrid+for+windows+forms&*


    Success
    Cor

    Monday, March 20, 2017 7:00 PM
  • Try this:

    Create a new Windows form project

    Add a datagridview. Don't rename it.

    Add this code to the code section of the form

    Dim table As DataTable
    
        Private Sub DataGridView1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
            Dim currentRow As DataRow = CType(BindingContext(DataGridView1.DataSource).Current, DataRowView).Row
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.Displayed Then
                    Continue For
                End If
                If currentRow("so_num") = CType(row.DataBoundItem, DataRowView).Row("so_num") Then
                    row.DefaultCellStyle.BackColor = Color.Pink
                Else
                    row.DefaultCellStyle.BackColor = Color.White
                End If
            Next
        End Sub
    
    
    
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            table = New DataTable
            Dim column As DataColumn
            With table
                column = New DataColumn With {
                    .ColumnName = "so_num",
                    .DataType = GetType(Integer)
                    }
                .Columns.Add(column)
    
                column = New DataColumn With {
                    .ColumnName = "so_line",
                    .DataType = GetType(Integer)
                    }
                .Columns.Add(column)
    
                Dim row As DataRow
                For soNum As Integer = 1 To 5000
                    row = .NewRow
                    row("so_num") = soNum
                    row("so_line") = 1
                    .Rows.Add(row)
    
                    row = .NewRow
                    row("so_num") = soNum
                    row("so_line") = 2
                    .Rows.Add(row)
                Next
            End With
    
            DataGridView1.AllowUserToAddRows = False
            DataGridView1.DataSource = table
        End Sub
    
    
    

    Tuesday, March 21, 2017 7:55 AM
  • Thank you for your reply

    On your example it is either M or F

    If drv.Item("Gender").ToString = "M" Then

    but in my case I can never know the sales order number ahead.

    It would be a combination of current_so and next_so

    if so_num = current_so then

    pink

    else

    white

    end if

    But how the current_so becomes the next_so is my problem

    and I can not figure it out.

    John


    John,

    Have a look at the .CellFormatting event:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting(v=vs.110).aspx

    On that page is an example - study that a bit and I think you'll see that you can use that for what you're wanting to do.

    I hope that helps.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, March 21, 2017 2:07 PM
  • Hi Bre-x,

    Based on your description, you want to change datagridview row back color by so-num column value, I put one datagridview control in Form and then change row back color by dataGridView1_DataBindingComplete event.

    Here is the sample that you can refer to.

    Code sample:

    Private Sub Form12_Load(sender As Object, e As EventArgs)
            loadstart()
        End Sub
        Private Sub loadstart()
            Dim dt As New DataTable()
            dt.Columns.Add("so_num", GetType(Integer))
            dt.Columns.Add("so_line", GetType(Integer))
            Dim dr1 As DataRow = dt.NewRow()
            dr1("so_num") = 2525
            dr1("so_line") = 1
            dt.Rows.Add(dr1)
    
            Dim dr2 As DataRow = dt.NewRow()
            dr2("so_num") = 2525
            dr2("so_line") = 2
            dt.Rows.Add(dr2)
    
            Dim dr3 As DataRow = dt.NewRow()
            dr3("so_num") = 2526
            dr3("so_line") = 1
            dt.Rows.Add(dr3)
    
            Dim dr4 As DataRow = dt.NewRow()
            dr4("so_num") = 2526
            dr4("so_line") = 2
            dt.Rows.Add(dr4)
    
            Dim dr5 As DataRow = dt.NewRow()
            dr5("so_num") = 2527
            dr5("so_line") = 3
            dt.Rows.Add(dr5)
            dataGridView1.DataSource = dt
        End Sub
        Private Sub dataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs)
            Dim first As Color = Color.White
            Dim second As Color = Color.Pink
            For Each drow As DataGridViewRow In dataGridView1.Rows
                If drow.Index <> -1 Then
                    Dim num As Integer = Convert.ToInt32(dataGridView1.Rows(drow.Index).Cells("so_num").Value)
                    If num = 2525 OrElse num = 2527 Then
                        dataGridView1.Rows(drow.Index).DefaultCellStyle.BackColor = first
                    Else
                        dataGridView1.Rows(drow.Index).DefaultCellStyle.BackColor = second
    
                    End If
                End If
            Next
    End Sub
    

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 22, 2017 6:18 AM
    Moderator
  • Thank you all,

    I will test each suggestion

    :)

    John

    Wednesday, March 22, 2017 1:58 PM