Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2005

Tip: Looking for answers? Try searching our database.

DataGridView.RowPrePaint - how to selectively paint the background

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JeremyHolt - 29 Dec 2005 13:40 GMT
Hi,

I am trying to change the background colour of the entire row based on the
contents of a value in a cell.

Using the examples in the MSDN documentation I have got thus far, however,
the foreground text is garbled:

   Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles
DataGridView1.RowPrePaint
       Dim backBrush As New
System.Drawing.SolidBrush(Me.GetRowColour(CType(sender, DataGridView),
e.RowIndex))
       Dim rowBounds As Rectangle = GetRowBounds(CType(sender,
DataGridView), e.RowBounds)
       Try
           e.Graphics.FillRectangle(backBrush, rowBounds)
           e.PaintParts = DataGridViewPaintParts.All And Not
DataGridViewPaintParts.ContentBackground
       Finally
           backBrush.Dispose()
       End Try
   End Sub

   Private Function GetRowBounds(ByVal Grid As DataGridView, ByVal
RowBounds As Rectangle) As Rectangle
       Return New Rectangle(Grid.RowHeadersWidth, RowBounds.Top,
Grid.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) -
Grid.HorizontalScrollingOffset + 1, RowBounds.Height)
   End Function

   Private Function GetRowColour(ByVal Grid As DataGridView, ByVal rowIndex
As Integer) As Drawing.Color
       Dim IsRoasted As Object = Grid.Rows(rowIndex).Cells(5).Value
       Dim IsOrganic As Object = Grid.Rows(rowIndex).Cells(4).Value
       Dim IsProduction As Object = Grid.Rows(rowIndex).Cells(3).Value
       If CBool(IsRoasted) Then
           Return Color.Red
       ElseIf CBool(IsOrganic) Then
           Return Color.Green
       ElseIf CBool(IsProduction) Then
           Return Color.Beige
       End If
   End Function

Could someone suggest what I missed here?

Many thanks
Jeremy Holt
Bart Mermuys - 30 Dec 2005 00:32 GMT
Hi,

> Hi,
>
[quoted text clipped - 3 lines]
> Using the examples in the MSDN documentation I have got thus far, however,
> the foreground text is garbled:

It looks ok to me, i would only change a couple of things, instead of
excluding DGVPaintPart.ContentBackground i would exclude
DGVPaintPart.Background, because eg. DGVButtonCell paints its button for
DGVPaintPart.ContentBackground.

Second,  more important i would add a Catch to see if any exceptions are
thrown, they can mess up the rest of the painting.

Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object,  ByVal
e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles
DataGridView1.RowPrePaint
 Try
   If ( (e.State And DataGridViewElementStates.Selected) =
DataGridViewElementStates.None) Then
     Dim backBrush As New
System.Drawing.SolidBrush(Me.GetRowColour(CType(sender, DataGridView),
e.RowIndex))
     Dim rowBounds As Rectangle = _
          GetRowBounds(CType(sender, DataGridView), e.RowBounds)

     e.Graphics.FillRectangle(backBrush, rowBounds)
     backBrush.Dispose()

     e.PaintHeader( False ) ' because background is excluded
     e.PaintParts = DataGridViewPaintParts.All And Not
DataGridViewPaintParts.Background
   End If
 Catch er As Exception
    Console.WriteLine( er.Message )    ' watch if no exception occured
 End Try
End Sub

HTH,
Greetings

>    Private Function GetRowBounds(ByVal Grid As DataGridView, ByVal
> RowBounds As Rectangle) As Rectangle
[quoted text clipped - 22 lines]
> Many thanks
> Jeremy Holt

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.