Background colour of column in datagrid

  • Thread starter Thread starter PJ
  • Start date Start date
P

PJ

How can you set the background color of individual
columns in a datagrid to be different to others? The
table styles only allow you to do this at the grid level
for all columns.
 
This was helpful, thanks. But to add to the question, I
want to be able to set different colors for the column
header cells also and I don't think the tips cover this.
Any ideas?
 
Hm, I didn't find this. Although I guess it must be possible by using the
Paint-event of the DataGrid.
I used this function to write something to some row-headers. I guess the
same must be possible with the column-headers:
Private Sub dbgFolder_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles dbgFolder.Paint

Dim row As Integer

row = TopRow()

Dim yDelta As Integer

If row >= 0 Then

yDelta = (dbgFolder.GetCellBounds(row, 0).Height + 1)

Dim y As Integer

y = (dbgFolder.GetCellBounds(row, 0).Top + 2)

Dim cm As CurrencyManager

cm = CType(Me.BindingContext(dbgFolder.DataSource,
dbgFolder.DataMember), CurrencyManager)

Dim dtvGrid As DataView

dtvGrid = New DataView(dtblGrid)

'5 omdat em anders de onderste telkens ni tekende...

Do While ((y < (dbgFolder.Height - yDelta + 3)) AndAlso (row <
cm.Count))

'get & draw the header text...

If DateDiff(DateInterval.Hour,
dtvGrid.Item(row).Item("DateFile"), Now) <= 12 Then

Dim text1 As String

' text1 = System.String.Format("row{0}", row)

text1 = System.String.Format("N", row)

e.Graphics.DrawString(text1, dbgFolder.Font, New
SolidBrush(Color.Black), 4, y)

End If

y = (y + yDelta)

row = (row + 1)

Loop

End If

End Sub





Or maybe override the Paint-event of the cell (I guess that's better, hehe),
and than use

g.FillRectangle(e.BackBrush, bounds)

But to be hon,nest I'm not really an expert of these things. I did some
stuff in it to extend the datagrid, but that's all :-)



Hope this helps,



Pieter
 
Yes, it helped thanks. I am just a little surprised at
the coding necessary just to alter the background color
of a column header. This really should be a property of
the DataGridColumnStyle class.
 
It is very good but how can I autosize the rowheights in my datagrid by
VB.NET (not C# , I can't convert C# to VB.NET on this page)?
 
Back
Top