Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Row or Cell Formating 1

Status
Not open for further replies.

bsimbeck

Programmer
May 2, 2003
44
US
I have a datagrid on a form. I need to change the foreground (ie text color) for either the entire row or one cell in that row based on the data that is in that row. for example I have a record that says we have 5 CD's left if the QTY column of the row is below ten I need to have the row (or a particular cell within that row) turn red.

Any ideas because I am stumped.

If a datagrid isn't the best way to go I would love to hear ideas or other data structures.

Thanks in advance,

Branden
 
Hi bsimbeck

I was wondering if you ever figured out how to do the above question? I'm looking for how to change sertine cells depending on there value... Any help would be great.. Thanks..
 
Override DataGridTextBoxColumn Paint event, something like this

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)

'Instantiate a DataRowView so we can examine a row
'of the DataView associated with the DataGrid
Dim drv As DataRowView
'Get a handle on the DataView being used by the CurrencyManager
Dim dv As DataView = CType(source.List, DataView)
'Get the DataRowView of the row being painted.
drv = CType(dv.Item(rowNum), DataRowView)
'Used column of the DataRowView to determine the formatting of the
'column this object is painting.
If drv("Month") = 10 Then
' Here is where we are going to do the actual painting.
backBrush = Brushes.Yellow
foreBrush = Brushes.Red
End If

MyBase.Paint(g, bounds, source, rowNum, _
backBrush, foreBrush, alignToRight)
End Sub



 
Hi PankajBanga

I'll try it out and let you know how it goes... Thanks...

 
Hi PankajBanga

I tried out the code.. I get "'Paint' conflicts with a event by the same name declared in Control." Haven't located as of yet were the other "Paint" is... Hmmm...

 
Hay Bigfoot..

I wouldn't mind seeing your projext on this..

 
Ok, you'll have to create custom control which inherits form DataGridTextBoxColumn and then override its Paint method, as follows:

Public Class ColoredTextBoxColumn
Inherits DataGridTextBoxColumn

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)

'Instantiate a DataRowView so we can examine a row
'of the DataView associated with the DataGrid
Dim drv As DataRowView
'Get a handle on the DataView being used by the CurrencyManager
Dim dv As DataView = CType(source.List, DataView)
'Get the DataRowView of the row being painted.
drv = CType(dv.Item(rowNum), DataRowView)
'Used column of the DataRowView to determine the formatting of the
'column this object is painting.
If drv("Month") = 10 Then
' Here is where we are going to do the actual painting.
backBrush = Brushes.Yellow
foreBrush = Brushes.Red
End If

MyBase.Paint(g, bounds, source, rowNum, _
backBrush, foreBrush, alignToRight)

End Class
 
Hey ICTECH. Where can I send it? It has 2 classes in it. More then I can post.
I am planning to write a FAQ on this as promised.
The grid is an ausome product when you can get your head around it.
 
Hi Big foot could you send your project to me as well?
stephenhughes@hotmail.co.uk

PankajBanga when I use that Method my form collaspes with 'No Symbols Displayed errors'



 
Hi PankajBanga

I'm having a laps of memory, how do I call this Class again.. Boy do I need holidays.. Thanks...


 
Hay Bigfoot..

you can Send it to me at rrider@wsd1.org

Thanks...
 
Hay PankajBanga

Disregard my last message.. It hit me after.. But Its not changing the color.. Its acting as if its not even there... Got to love things that make you go hmmmm....
 
I sent then out just now. Let me know if you need anything else.
 
Hay Bigfoot...

How big is the file? My MailBox is 30MB..

 
hay Bigfoot...

Thanks for trying.. I finally got it to work.. It struck me what I was doig wrong.. Thanks again...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top