DatagridView modify individual Column Header cells

G

Guest

I am have a data grid view, with 2 columns for each day of the week:

Monday | Monday | Tuesday | Tuesday| etc
Hours | Tasks | Hours | tasks |
___________________________________|
3 | 4 | 5 |7 |
2 | 3 | 4 |6 |


It would be nice if the header could split accross 2 cells, like this,

Monday | Tuesday | etc
Hours | Tasks | Hours | tasks |
___________________________________|
3 | 4 | 5 |7 |
2 | 3 | 4 |6 |


I see that datagridview columns have an AdjustCellBorderStyle() function,
but the documentation seems to suggest that you have to override this
function to use it.

I tried the example here:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewadvancedborderstyle.aspx

and successfully got custom header cell borders, but since

Public Overrides Function AdjustColumnHeaderBorderStyle

is a function in the CustomDataGridView class, ALL my Header cells use the
custom settings.

I tried adding the function to the DataGridViewCustomColumn Class, but
DataGridViewColumn does not have such a function to override.

however the DataGridViewColumnHeaderCell does have , so I tried this:

Public Class DataGridViewCustomColumn
Inherits DataGridViewColumn

Public Sub New()
Me.CellTemplate = New DataGridViewCustomCell()
Me.HeaderCell = New DataGridViewCustomHeaderCell
End Sub

End Class

Public Class DataGridViewCustomHeaderCell
Inherits DataGridViewColumnHeaderCell

Public Overrides Function AdjustCellBorderStyle( _
ByVal dataGridViewAdvancedBorderStyleInput As
DataGridViewAdvancedBorderStyle, _
ByVal dataGridViewAdvancedBorderStylePlaceHolder As
DataGridViewAdvancedBorderStyle, _
ByVal singleVerticalBorderAdded As Boolean, _
ByVal singleHorizontalBorderAdded As Boolean, _
ByVal firstVisibleColumn As Boolean, _
ByVal firstVisibleRow As Boolean) As
DataGridViewAdvancedBorderStyle

If firstVisibleRow Then
dataGridViewAdvancedBorderStylePlaceHolder.Top = _
DataGridViewAdvancedCellBorderStyle.InsetDouble
Else
dataGridViewAdvancedBorderStylePlaceHolder.Top = _
DataGridViewAdvancedCellBorderStyle.None
End If
dataGridViewAdvancedBorderStylePlaceHolder.Right =
DataGridViewAdvancedCellBorderStyle.None

With dataGridViewAdvancedBorderStylePlaceHolder
.Right = dataGridViewAdvancedBorderStyleInput.Right
.Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
End With

Return dataGridViewAdvancedBorderStylePlaceHolder
End Function
End Class

This gives no errors, but the AdjustCellBorderStyle function in my
DataGridViewCustomHeaderCell is never called.

What am I missing ?
 
L

Linda Liu [MSFT]

Hi Marty,

Thank you for posting.

When a DataGridView is painting a column header cell, it will call the
DataGridView.AdjustColumnHeaderBorderStyle method to adjust the
DataGridViewAdvancedBorderStyle for this column header cell.

Even if you have set your custom column header cell to the DataGridView and
override the AdjustCellBorderStyle method in the custom column header cell
class, the DataGridView won't call the AdjustCellBorderStyle method in the
custom column header cell class when it paints column header cells.

If you want to customize the border style of the column header, you should
override the DataGridView.AdjustColumnHeaderBorderStyle method.

If you have anything unclear, please don't hesitate to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Linda,
I think I must be missing something !

I have overridden the DataGridView.AdjustColumnHeaderBorderStyle method -
the example microsoft code does this. However ( as far as I can see) this
will be called when every column is painted and therefore every column will
use the same custom style. I need some column headers to have one syle, and
others to have a different style. There are boolean parameters passed to the
DataGridView.AdjustColumnHeaderBorderStyle method for firstDisplayedColumn
and lastVisibleColumn, so you can set different properties for the first and
last columns, but this doesn't help me.
Is it possible to workout which column is being painted from inside the
DataGridView.AdjustColumnHeaderBorderStyle method ? Or have I missed the
point completely ?
 
L

Linda Liu [MSFT]

Hi Marty,

Thank you for your response.

I'm sorry but I don't think we could workout which column is being painted
from inside the DataGridView.AdjustColumnHeaderBorderStyle method.

An alternative is to handle the DataGridView's CellPainting event or
override the OnCellPainting method in your class derived from the
DataGridView . Through the DataGridViewCellPaintingEventArgs, you could
access the row index and column index of the cell being painted.

The following is a sample.

// handle the datagridview's CellPainting event
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal
e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If (e.RowIndex = -1 And e.ColumnIndex = 1) Then
e.AdvancedBorderStyle.All =
DataGridViewAdvancedCellBorderStyle.None
End If
End Sub

or

Public Class MyDataGridView
Inherits DataGridView

// override the OnCellPainting method in the derived class
Protected Overrides Sub OnCellPainting(ByVal args As
DataGridViewCellPaintingEventArgs)

If (args.RowIndex = -1 And args.ColumnIndex = 1) Then
args.AdvancedBorderStyle.All =
DataGridViewAdvancedCellBorderStyle.None
End If
MyBase.OnCellPainting(args)

End Sub

End Class

Hope this helps.

FYI, you could go to MSDN Product Feedback Center to make a suggestion on
adding a parameter in the AdjustColumnHeaderBorderStyle method to identify
which column is being painted. The following is the link:
http://lab.msdn.microsoft.com/productfeedback/default.aspx

If you have anything unclear, please don't hesitate to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Marty,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If the problem is not resolved or you have anything unclear, please feel
free to post in the newsgroup and we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support

============================================================================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.

============================================================================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top