Results 1 to 5 of 5

Thread: datagrid row backcolor

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    11

    datagrid row backcolor

    i have a datagrid that is bound to a dataview. is there a way to have the rows in the datagrid change the backcolor depending on the value of a column in the dataview???

    for example - rows that column "balance" < 0 should appear with red backcolor, while rows with column "balance" >= 0 should appear with green backcolor?

    similarly, is there a way to change the row's font depending on the value of a column in the dataview? for example: rows that column "days past due" > 90 should appear with bold fonts, while other rows with regular unbolded font?

  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316

    If your desperate, very bright and ambitious

    I don't think you going to have much luck getting the packaged datagrid to do this. Depending on your ambition you could create your own datagrid. VB.Net allows you to create your own controls with their own functionality. Check it out by going to your help index and typing in "Walkthroughs", select the walkthrough called "control creation". You can also search for "user controls" in the index. With that said, I have never tried creating my own control, but am considering it as a learning experience. Maybe someone else out there knows a way to get the packaged datagrid to behave this way. Good luck.
    SCUZ

  3. #3
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76
    Yes it's possible. I've built my own datagrid based on the VB-datagrid.

    You have to override the Paint-method, example from MS:
    Code:
    Public Class Form1
       Inherits Form
       Protected dataGrid1 As DataGrid
       Protected myDataSet As DataSet
        
       Private Sub PaintCell(sender As Object, e As MouseEventArgs)
          ' Use the HitTest method to get a HitTestInfo object.
          Dim hi As DataGrid.HitTestInfo
          Dim grid As DataGrid = CType(sender, DataGrid)
          hi = grid.HitTest(e.X, e.Y)
          ' Test if the clicked area was a cell.
          If hi.Type = DataGrid.HitTestType.Cell Then
             ' If it's a cell, get the GridTable and ListManager of the
             ' clicked table.         
             Dim dgt As DataGridTableStyle = dataGrid1.TableStyles(0)
             Dim cm As CurrencyManager = CType(Me.BindingContext _
             (myDataSet.Tables(dgt.MappingName)), CurrencyManager)
             ' Get the Rectangle of the clicked cell.
             Dim cellRect As Rectangle
             cellRect = grid.GetCellBounds(hi.Row, hi.Column)
             ' Get the clicked DataGridTextBoxColumn.
             Dim gridCol As MyGridColumn = CType _
             (dgt.GridColumnStyles(hi.Column), MyGridColumn)
             ' Get the Graphics object for the form.
             Dim g As Graphics = dataGrid1.CreateGraphics()
             ' Create two new Brush objects: a fore brush and back brush.
             Dim fBrush As New SolidBrush(Color.Blue)
             Dim bBrush As New SolidBrush(Color.Yellow)
             ' Invoke the Paint method to paint the cell with the brushes.
             gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush, False)
          End If
       End Sub 'PaintCell
    End Class 'Form1 
    
    Public Class MyGridColumn
    Inherits DataGridTextBoxColumn
       Public Sub PaintCol(g As Graphics , cellRect As Rectangle , _
          cm As CurrencyManager , rowNum As integer , bBrush as Brush , _
          fBrush As Brush , isVisible As Boolean )
          me.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible)
       End Sub
    End Class
    ________________________
    Fredrik Klarqvist

  4. #4
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    Thanks for the code, while were on the topic of datagrids do you have some code that would allow me to resize individual columns?
    SCUZ

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    11
    use datagridcolumnstyles collection - and assign each column the desied width and data member to be associated to a column in the dataview the tablestyle is connected to.

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