Just finished the "Colored Part" of the GridColumnStyle. I've created 2 components :
To customize the fore- and background color of a cell I have overriden the paint event of the DataGridTextBoxColumn. The fore- and background color can be changed by setting the properties:
- ForeGroundColor
- BackGroundColor
- ForeGroundColorOnFocus
- BackGroundColorOnFocus
I've also implemented two properties who set the fore- and background color of the entire row containing the cell with focus.
- GridRowBackColorOnFocus
- GridRowForeColorOnFocus
Below an example of how to use the properties and the code implemented in the Paint event of the DataGridTextBoxColumn.
Paint Event
'Override paint event and set the foreBrush and/or backbrush
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)
Dim lb_PaintingCellWithFocus As Boolean Dim lb_PaintingRowWithFocus As Boolean Dim lI_ColoredDataGrid As IColoredDataGridInterface
'If cell on current row
If Me.DataGridTableStyle.DataGrid.CurrentRowIndex = rowNum Then lb_PaintingRowWithFocus = True
'If cell is current cell If Me.DataGridTableStyle.GridColumnStyles.IndexOf(Me) = Me.DataGridTableStyle.DataGrid.CurrentCell.ColumnNumber Then lb_PaintingCellWithFocus = True End If
End If
'If cell is current cell
If lb_PaintingCellWithFocus Then If Not iColor_BackGroundFocus.IsEmpty Then backBrush = New System.Drawing.SolidBrush(iColor_BackGroundFocus) End If If Not iColor_ForeGroundFocus.IsEmpty Then foreBrush = New System.Drawing.SolidBrush(iColor_ForeGroundFocus) End If Else
'If cell in selected row If lb_PaintingRowWithFocus Then If TypeOf (Me.DataGridTableStyle) Is IColoredDataGridInterface Then lI_ColoredDataGrid = DirectCast(Me.DataGridTableStyle, IColoredDataGridInterface) If iColor_BackGroundFocus.IsEmpty Then If Not iColor_BackGround.IsEmpty Then backBrush = New System.Drawing.SolidBrush(iColor_BackGround) End If Else backBrush = New System.Drawing.SolidBrush (lI_ColoredDataGrid.GridRowBackColorOnFocus) End If If iColor_ForeGroundFocus.IsEmpty Then If Not iColor_ForeGround.IsEmpty Then foreBrush = New System.Drawing.SolidBrush(iColor_ForeGround) End If Else foreBrush = New System.Drawing.SolidBrush (lI_ColoredDataGrid.GridRowForeColorOnFocus) End If End If Else If Not iColor_BackGround.IsEmpty Then backBrush = New System.Drawing.SolidBrush(iColor_BackGround) End If If Not iColor_ForeGround.IsEmpty Then foreBrush = New System.Drawing.SolidBrush(iColor_ForeGround) End If End If End If
'Make sure the base class gets called to do the drawing with 'the possibly changed brushes MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
If lb_PaintingCellWithFocus Then If Not iColor_ForeGroundFocus.IsEmpty Then Me.TextBox.ForeColor = iColor_ForeGroundFocus End If If Not iColor_BackGroundFocus.IsEmpty Then Me.TextBox.BackColor = iColor_BackGroundFocus End If
'Do not do this in editmode otherwise cursor will move to first pos If Me.ReadOnly Then Me.TextBox.Select(0, 0) End If End If
End Sub
Example of use
Private iTS_MyTableStyle As New ColoredDataGridTableStyle
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lCS_MyColumnStyle As ColoredDataGridTextBoxColumn
--Set the datasource and datamember Me.DataGrid1.DataSource = Me.DS_Employees1 Me.DataGrid1.DataMember = "Employees"
iTS_MyTableStyle.MappingName = "Employees" iTS_MyTableStyle.ReadOnly = True iTS_MyTableStyle.GridRowBackColorOnFocus = System.Drawing.Color.Khaki iTS_MyTableStyle.GridRowForeColorOnFocus = System.Drawing.Color.Green
--Add three columns and set the color properties lCS_MyColumnStyle = New ColoredDataGridTextBoxColumn lCS_MyColumnStyle.BackColorOnFocus = System.Drawing.Color.Wheat lCS_MyColumnStyle.ForeColorOnFocus = System.Drawing.Color.Turquoise lCS_MyColumnStyle.BackColor = System.Drawing.Color.SpringGreen lCS_MyColumnStyle.ForeColor = System.Drawing.Color.Orange lCS_MyColumnStyle.ReadOnly = False lCS_MyColumnStyle.MappingName = Me.DS_Employees1.Employees.CityColumn.ColumnName
iTS_MyTableStyle.GridColumnStyles.Add(lCS_MyColumnStyle)
lCS_MyColumnStyle = New ColoredDataGridTextBoxColumn lCS_MyColumnStyle.BackColorOnFocus = System.Drawing.Color.Red lCS_MyColumnStyle.ForeColorOnFocus = System.Drawing.Color.SteelBlue lCS_MyColumnStyle.ReadOnly = False lCS_MyColumnStyle.MappingName = Me.DS_Employees1.Employees.CountryColumn.ColumnName
iTS_MyTableStyle.GridColumnStyles.Add(lCS_MyColumnStyle)
lCS_MyColumnStyle = New ColoredDataGridTextBoxColumn lCS_MyColumnStyle.BackColorOnFocus = System.Drawing.Color.Pink lCS_MyColumnStyle.ForeColorOnFocus = System.Drawing.Color.Black lCS_MyColumnStyle.ReadOnly = False lCS_MyColumnStyle.MappingName = Me.DS_Employees1.Employees.HomePhoneColumn.ColumnName
iTS_MyTableStyle.GridColumnStyles.Add(lCS_MyColumnStyle)
--Add the custom tablestyle to tablestyles Me.DataGrid1.TableStyles.Add(iTS_MyTableStyle) --Fill the grid with data Me.SqlDA_Employees.Fill(Me.DS_Employees1)
End Sub
If anyone is interested in the full code or the library, mail me.
10:32:57 PM
|