Question Setting row color in datagridview...

jak

Member
Joined
Feb 21, 2009
Messages
19
Programming Experience
Beginner
Hello Experts,
I hav a DGV which shows data of my transaction_table. There r 4 columns ID, NAME, DUE_DATE and STATUS. Let ID is 1, NAME is jak, DUE_DATE is 25.02.2009 and status is notpaid(by deafault). Now , I want to set row color according to following condition...

if DUE_DATE >= current date(today's) and status="notpaid"
then set row color= green
if else
DUE_DATE < current date(today's) and status="paid"
then set row color= green
else
set row color = red

Bcoz i m newbi tht's why i really don't hav any idea abt thid. please guide me with proper coding.
I m using VS 2008, SQLSERVER with visiual basic...

THANXXXX IN ADVANCE....
 
instead of the two ifs statements, couldnt you just use:

if due_date >= current data OR status = "paid" then
Item.backcolor = green
else
Item.backcolor = red
end if

then if the user has payed and it was not due yet, they wont get a red ^^

i havn't been able to try using a datagridview yet
can you not find some way to its Backcolour then?
 
Something like this should do it for you. Should only turn rows Red that are past due and not paid.

VB.NET:
	Private Sub ColorRows(ByVal dgv As DataGridView)

		If dgv Is Nothing Then
			Return
		End If

		For Each row As DataGridViewRow In dgv.Rows
			If CType(row.Cells.Item("DueDate").Value, DateTime) < DateTime.Now _
			 OrElse row.Cells.Item("Status").Value Is "Paid" Then
				row.DefaultCellStyle.BackColor = Color.LightGreen
			Else
				row.DefaultCellStyle.BackColor = Color.Red
			End If
		Next

	End Sub
 

Latest posts

Back
Top