Results 1 to 6 of 6

Thread: Datagrid cell coloring

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Datagrid cell coloring

    I am trying to color individual cells on a datagrid. I am unsure whether you actually can or not but here is what i have. I have a datagrid that gets filled by a dataset upon form loading. I also search the cells one by one for the number 376 and I am trying to color those cells. I can get it to color rows but not a cell. If it cannot be done, or you know of another control that would allow for something like this, please let me know. Here is the code.

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         SqlDataAdapter1.Fill(DataSet11)
    3.         Dim x As Integer
    4.         Dim y As Integer
    5.         Dim CellData As String
    6.         x = 4
    7.         y = 4
    8.         While x < 7
    9.             While y < 7
    10.  
    11.                 CellData = DataGrid1.Item(x, y) & ""
    12.  
    13.                 If CellData = "" Then
    14.                     y = y + 1
    15.                 Else
    16.                     If CellData = "376" Then
    17.                         DataGrid1.BackgroundColor = System.Drawing.Color.Blue
    18.                     End If
    19.                     y = y + 1
    20.                 End If
    21.  
    22.             End While
    23.             y = 4
    24.             x = x + 1
    25.         End While
    26.  
    27.     End Sub

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683
    Forget all that crap I wrote before. I figured it out. You have to overwrite the paint code for the datagrid. Took awhile and a couple of classes and some code, its all done. I also can move columns, rename, resize. Its rather cool. Thanks for all the posts.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683
    I am getting private msgs for the code. Please do not msg me for the code. I will post it here. It is the form and 2 classes. This is a variation of a custom datagrid on msdn. If you do a search on msdn for custom datagrid you will find it. It has other options that are not of use to me.

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.    
    4.     'variables for brush colors
    5.     Private asqn As Brush
    6.     Private bsqn As Brush
    7.     Private fsqn As Brush
    8.     Private gsqn As Brush
    9.     Private ops As Brush
    10.     Private spsqn As Brush
    11.     Private other As Brush
    12.     Private vor As Brush
    13.     Private hitRow As Integer
    14.     Private toolTip1 As System.Windows.Forms.ToolTip
    15.  
    16. #Region " Windows Form Designer generated code "
    17.  
    18.        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         'Brush colors
    20.         Me.asqn = Brushes.DeepPink
    21.         Me.bsqn = Brushes.DarkOrange
    22.         Me.fsqn = Brushes.DarkSeaGreen
    23.         Me.gsqn = Brushes.Green
    24.         Me.ops = Brushes.Pink
    25.         Me.spsqn = Brushes.Brown
    26.         Me.vor = Brushes.Red
    27.         'fill datagrid
    28.         Me.SqlDataAdapter1.Fill(Me.DataSet11)
    29.         'create datagridstyle and set to sql table
    30.         Dim tableStyle As New DataGridTableStyle
    31.         tableStyle.MappingName = "VehJul"
    32.  
    33.         'create columns for datagrid using class
    34.         Dim column As New FormattableTextBoxColumn
    35.         column.MappingName = "AssetID"
    36.         column.HeaderText = "ID"
    37.         column.Width = 40
    38.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    39.         tableStyle.GridColumnStyles.Add(column)
    40.  
    41.         column = New FormattableTextBoxColumn
    42.         column.MappingName = "Asset"
    43.         column.HeaderText = "Asset"
    44.         column.Width = 130
    45.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    46.         tableStyle.GridColumnStyles.Add(column)
    47.  
    48.  
    49.         column = New FormattableTextBoxColumn
    50.         column.MappingName = "Notes"
    51.         column.HeaderText = "Notes"
    52.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    53.         tableStyle.GridColumnStyles.Add(column)
    54.  
    55.  
    56.         column = New FormattableTextBoxColumn
    57.         column.MappingName = "1"
    58.         column.HeaderText = "1"
    59.         column.Width = 20
    60.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    61.         tableStyle.GridColumnStyles.Add(column)
    62.  
    63.  
    64.         column = New FormattableTextBoxColumn
    65.         column.MappingName = "2"
    66.         column.HeaderText = "2"
    67.         column.Width = 20
    68.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    69.         tableStyle.GridColumnStyles.Add(column)
    70.  
    71.  
    72.         column = New FormattableTextBoxColumn
    73.         column.MappingName = "3"
    74.         column.HeaderText = "3"
    75.         column.Width = 20
    76.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    77.         tableStyle.GridColumnStyles.Add(column)
    78.  
    79.  
    80.         column = New FormattableTextBoxColumn
    81.         column.MappingName = "4"
    82.         column.HeaderText = "4"
    83.         column.Width = 20
    84.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    85.         tableStyle.GridColumnStyles.Add(column)
    86.  
    87.         'column = New FormattableTextBoxColumn
    88.         column.MappingName = "5"
    89.         column.HeaderText = "5"
    90.         column.Width = 20
    91.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    92.         tableStyle.GridColumnStyles.Add(column)
    93.  
    94.         column = New FormattableTextBoxColumn
    95.         column.MappingName = "6"
    96.         column.HeaderText = "6"
    97.         column.Width = 20
    98.         AddHandler column.SetCellFormat, AddressOf FormatGridRow
    99.         tableStyle.GridColumnStyles.Add(column)
    100.  
    101.         'Add to the datagrid
    102.         Me.DataGrid1.TableStyles.Add(tableStyle)
    103.     End Sub
    104.  
    105. #Region "columnstyle event handlers"
    106.  
    107.    
    108.  
    109.     Private Sub FormatGridRow(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
    110.         ' Conditionally set properties in e depending upon e.Row and e.Col.
    111.         Dim discontinued As Boolean = CBool(IIf(e.Column <> 0, Me.DataGrid1(e.Row, 0), e.CurrentCellValue)) 'TODO: For performance reasons this should be changed to nested IF statements
    112.  
    113.         Dim a As String
    114.         a = e.CurrentCellValue
    115.           If a = "A" Then e.BackBrush = asqn
    116.         If a = "B" Then e.BackBrush = bsqn
    117.         If a = "F" Then e.BackBrush = fsqn
    118.         If a = "G" Then e.BackBrush = gsqn
    119.         If a = "O" Then e.BackBrush = ops
    120.         If a = "S" Then e.BackBrush = spsqn
    121.         If a = "V" Then e.BackBrush = vor
    122.         If a = "N" Then e.BackBrush = other
    123.  
    124.     End Sub
    125.  
    126. #End Region
    127.  
    128.  
    129. #Region "Helper Methods"
    130.     ' Forces a repaint of given row.
    131.     Private Sub RefreshRow(ByVal row As Integer)
    132.         Dim rect As Rectangle = Me.DataGrid1.GetCellBounds(row, 0)
    133.         rect = New Rectangle(rect.Right, rect.Top, Me.DataGrid1.Width, rect.Height)
    134.         Me.DataGrid1.Invalidate(rect)
    135.     End Sub 'RefreshRow
    136.  
    137.     ' Look back up in the project path for file...
    138.     Private Function GetPathTo(ByVal name As String) As String
    139.         Dim i As Integer
    140.         For i = 0 To 3
    141.             If System.IO.File.Exists(name) Then
    142.                 Exit For
    143.             End If
    144.             name = "..\" + name
    145.         Next i
    146.         Return name
    147.     End Function 'GetPathTo
    148.  
    149. #End Region
    150. End Class

    and add a class named datagridformatcelleventargs
    VB Code:
    1. Public Delegate Sub FormatCellEventHandler(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
    2.  
    3. Public Class DataGridFormatCellEventArgs
    4.     Inherits EventArgs
    5.  
    6.     Public Sub New(ByVal row As Integer, ByVal col As Integer, ByVal cellValue As Object)
    7.         rowNum = row
    8.         colNum = col
    9.         fontVal = Nothing
    10.         backBrushVal = Nothing
    11.         foreBrushVal = Nothing
    12.         fontDisposeVal = False
    13.         backBrushDisposeVal = False
    14.         foreBrushDisposeVal = False
    15.         useBaseClassDrawingVal = True
    16.         currentCellValueVal = cellValue
    17.     End Sub 'New
    18.  
    19.     ' Holds the column number of the cell being painted.
    20.     Public Property Column() As Integer
    21.         Get
    22.             Return colNum
    23.         End Get
    24.         Set(ByVal Value As Integer)
    25.             colNum = Value
    26.         End Set
    27.     End Property
    28.  
    29.     ' Holds the row number of the cell being painted.
    30.     Public Property Row() As Integer
    31.         Get
    32.             Return rowNum
    33.         End Get
    34.         Set(ByVal Value As Integer)
    35.             rowNum = Value
    36.         End Set
    37.     End Property
    38.  
    39.     ' Holds the font to be used to draw text in the cell.
    40.     Public Property TextFont() As Font
    41.         Get
    42.             Return fontVal
    43.         End Get
    44.         Set(ByVal Value As Font)
    45.             fontVal = Value
    46.         End Set
    47.     End Property
    48.  
    49.     ' Holds the brush used to paint the cell's background.
    50.     Public Property BackBrush() As Brush
    51.         Get
    52.             Return backBrushVal
    53.         End Get
    54.         Set(ByVal Value As Brush)
    55.             backBrushVal = Value
    56.         End Set
    57.     End Property
    58.  
    59.     ' Holds the brush used to paint the text in the cell.
    60.     Public Property ForeBrush() As Brush
    61.         Get
    62.             Return foreBrushVal
    63.         End Get
    64.         Set(ByVal Value As Brush)
    65.             foreBrushVal = Value
    66.         End Set
    67.     End Property
    68.  
    69.     ' Set to true if the Dispose method of the TextFont
    70.     '     should be called by the Paint override.
    71.     Public Property TextFontDispose() As Boolean
    72.         Get
    73.             Return fontDisposeVal
    74.         End Get
    75.         Set(ByVal Value As Boolean)
    76.             fontDisposeVal = Value
    77.         End Set
    78.     End Property
    79.  
    80.     ' Set to true if the Dispose method of the BackBrush
    81.     '     should be called by the Paint override.
    82.     Public Property BackBrushDispose() As Boolean
    83.         Get
    84.             Return backBrushDisposeVal
    85.         End Get
    86.         Set(ByVal Value As Boolean)
    87.             BackBrushDispose = Value
    88.         End Set
    89.     End Property
    90.  
    91.     ' Set to true if the Dispose method of the ForeBrush
    92.     '     should be called by the Paint override.
    93.     Public Property ForeBrushDispose() As Boolean
    94.         Get
    95.             Return foreBrushDisposeVal
    96.         End Get
    97.         Set(ByVal Value As Boolean)
    98.             ForeBrushDispose = Value
    99.         End Set
    100.     End Property
    101.  
    102.     ' Set to false if the MyBase.Paint method  
    103.     '     should not be called in the Paint override.
    104.     Public Property UseBaseClassDrawing() As Boolean
    105.         Get
    106.             Return useBaseClassDrawingVal
    107.         End Get
    108.         Set(ByVal Value As Boolean)
    109.             useBaseClassDrawingVal = Value
    110.         End Set
    111.     End Property
    112.  
    113.     ' Holds the current cell value.
    114.     Public ReadOnly Property CurrentCellValue() As Object
    115.         Get
    116.             Return currentCellValueVal
    117.         End Get
    118.     End Property
    119.  
    120.     ' Private fields to hold the public properties.
    121.     Private colNum As Integer
    122.     Private rowNum As Integer
    123.     Private fontVal As Font
    124.     Private backBrushVal As Brush
    125.     Private foreBrushVal As Brush
    126.     Private fontDisposeVal As Boolean
    127.     Private backBrushDisposeVal As Boolean
    128.     Private foreBrushDisposeVal As Boolean
    129.     Private useBaseClassDrawingVal As Boolean
    130.     Private currentCellValueVal As Object
    131.  
    132. End Class

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683
    and add a class named formattabletextboxcolumn
    VB Code:
    1. Public Class FormattableTextBoxColumn
    2.     Inherits DataGridTextBoxColumn
    3.  
    4.     Public Event SetCellFormat As FormatCellEventHandler
    5.  
    6.     'used to fire an event to retrieve formatting info
    7.     'and then draw the cell with this formatting info
    8.     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)
    9.         Dim e As DataGridFormatCellEventArgs = Nothing
    10.  
    11.         'fire the formatting event
    12.         Dim col As Integer = Me.DataGridTableStyle.GridColumnStyles.IndexOf(Me)
    13.         e = New DataGridFormatCellEventArgs(rowNum, col, Me.GetColumnValueAtRow([source], rowNum))
    14.         RaiseEvent SetCellFormat(Me, e)
    15.  
    16.         Dim callBaseClass As Boolean = True ' assume we will call the baseclass
    17.  
    18.         If Not (e.BackBrush Is Nothing) Then
    19.             backBrush = e.BackBrush
    20.         End If
    21.         If Not (e.ForeBrush Is Nothing) Then
    22.             foreBrush = e.ForeBrush
    23.         End If
    24.  
    25.         'if TextFont set, then must call drawstring
    26.         If Not (e.TextFont Is Nothing) Then
    27.             g.FillRectangle(backBrush, bounds)
    28.             Try
    29.                 Dim charWidth As Integer = Fix(Math.Ceiling(g.MeasureString("c", e.TextFont, 20, StringFormat.GenericTypographic).Width))
    30.  
    31.                 Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()
    32.                 Dim maxChars As Integer = Math.Min(s.Length, bounds.Width / charWidth)
    33.  
    34.                 Try
    35.                     g.DrawString(s.Substring(0, maxChars), e.TextFont, foreBrush, bounds.X, bounds.Y + 2)
    36.                 Catch ex As Exception
    37.                     Console.WriteLine(ex.Message.ToString())
    38.                 End Try
    39.             Catch 'empty catch
    40.             End Try
    41.             callBaseClass = False
    42.         End If
    43.  
    44.         If Not e.UseBaseClassDrawing Then
    45.             callBaseClass = False
    46.         End If
    47.         If callBaseClass Then
    48.             MyBase.Paint(g, bounds, [source], rowNum, backBrush, foreBrush, alignToRight)
    49.         End If
    50.         'clean up
    51.         If Not (e Is Nothing) Then
    52.             If e.BackBrushDispose Then
    53.                 e.BackBrush.Dispose()
    54.             End If
    55.             If e.ForeBrushDispose Then
    56.                 e.ForeBrush.Dispose()
    57.             End If
    58.             If e.TextFontDispose Then
    59.                 e.TextFont.Dispose()
    60.             End If
    61.         End If
    62.     End Sub 'Paint    
    63. End Class

  5. #5
    Member
    Join Date
    Sep 2004
    Posts
    43
    hmmm... is there a way in print out an document from datagrid???

    hmm.. i know it gotta use printpriviewdialog and printDocument...hmm but how to go about doing it??
    -ralphz-

  6. #6
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    in your toolbox grab the printdocument control and drop it on your form. Add a button and place this code in the click event.

    PrintDocument1.Print()
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width