|
-
Oct 7th, 2002, 09:54 AM
#1
Thread Starter
New Member
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?
-
Oct 7th, 2002, 06:43 PM
#2
Hyperactive Member
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.
-
Oct 25th, 2002, 03:40 AM
#3
Lively Member
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
-
Oct 25th, 2002, 04:50 AM
#4
Hyperactive Member
Thanks for the code, while were on the topic of datagrids do you have some code that would allow me to resize individual columns?
-
Oct 25th, 2002, 05:13 AM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|