conditional change of a datagridviews cell background color and text
Asked Answered
E

3

7

This is for a winform vb.net 2008 app. I'm bringing data back from a database and based on some static conditions... i want to change the color of the background and the text. There is no rowdatabound event in winform...

hope someone can provide some guidance thanks shannon

Eveliaevelin answered 28/10, 2009 at 19:47 Comment(2)
I can think of at least 20 different ways to bring data back from a DB so it could be helpful if you were a little more specific.Cinchona
sorry about that... i'm not having trouble bringing data back from the db, it's formatting the style of the cell that the data is going into.Eveliaevelin
E
4

I got it working... in the RowPostPaint event.. If I put in

if (my criteria here)
    Me.dgTableInfo.Rows(e.RowIndex).Cells("ColumnName").Style.BackColor = Color.Red
end if
Eveliaevelin answered 28/10, 2009 at 20:29 Comment(0)
P
1

Don't forget to set the selectoinBackColor too... otherwise if your red row changes, but you have it highlighted, it will look just like all the rest anyway.

Pitt answered 4/11, 2009 at 19:15 Comment(0)
C
1

This works without creating or calling multiple subs or functions. Seems to work for every instance I need it to.

Do While myDataReader.Read()
    ItemID = Trim(myDataReader.Item("ITEM").ToString())
    PAR = myDataReader.Item("PAR").ToString()
    Returned = myDataReader.Item("RETURNED_AMOUNT")
    Taken = myDataReader.Item("TAKEN_AMOUNT")
    OnHand = ((PAR + Returned) - Taken)

    DataGridViewItemList.Rows.Add(ItemID, PAR, Returned, Taken, OnHand)

    RI = DataGridViewItemList.Rows.Count - 1
    If OnHand <= (PAR / 2) Then
        DataGridViewItemList.Rows(RI).DefaultCellStyle.BackColor = Color.DarkSalmon
    Else
        DataGridViewItemList.Rows(RI).DefaultCellStyle.BackColor = Nothing
    End If
Loop
Clearwing answered 8/6, 2011 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.