Changing datagridview cells borders at runtime

S

steve

Hi All

I urgently need help on setting datagridview cell borders at runtime

I found some code on the web from
Programming Smart Client Data Applications with .NET 2.0
by Brian Noyes
See below

This is what I have been trying to achieve, but when I run it ALL the cell
Top borders go from inset to feint white on ALL cells, ALL rows

If I change the line (marked *******) to false then the borders are OK but
obviously no hiding of the required cell top borders

I am completely lost on what is wrong

Any help greatly appreciated

Regards
Steve

Public Class GroupByGrid

Inherits DataGridView

Public Sub New()

With Me

..AdvancedCellBorderStyle.All = DataGridViewAdvancedCellBorderStyle.Inset

End With

End Sub

Protected Overrides Sub OnCellFormatting(ByVal args As
DataGridViewCellFormattingEventArgs)

' Call home to base

MyBase.OnCellFormatting(args)

' First row always displays

If args.RowIndex = 0 Then

Return

End If

If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then

args.Value = String.Empty

args.FormattingApplied = True

End If

End Sub

Private Function IsRepeatedCellValue(ByVal rowIndex As Integer, ByVal
colIndex As Integer) As Boolean

Dim currCell As DataGridViewCell = Rows(rowIndex).Cells(colIndex)

Dim prevCell As DataGridViewCell = Rows(rowIndex - 1).Cells(colIndex)

If Not IsNothing(currCell) AndAlso Not IsNothing(prevCell) AndAlso Not
IsNothing(currCell.Value) AndAlso Not IsNothing(prevCell.Value) Then

If (currCell.Value.ToString() = prevCell.Value.ToString() OrElse
currCell.Value.Equals(prevCell.Value)) And prevCell.Value.ToString <>
String.Empty And currCell.Value.ToString <> String.Empty Then

Return true **********************************************

Else

Return False

End If

Else

Return False

End If

End Function

Protected Overrides Sub OnCellPainting(ByVal args As
DataGridViewCellPaintingEventArgs)

MyBase.OnCellPainting(args)

' Ignore column and row headers and first row

If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then

Return

End If

If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then

args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None

Else

args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top

End If



End Sub

End Class
 
C

Cor Ligthert [MVP]

steve,

I gave you the solution that was used in past with a datagrid, did you try
that?

Cor
 
L

Linda Liu [MSFT]

Hi Steve,

Thank you for posting.

I perfomed a test based on your code and reproduce the problem you have
described. Through debugging, I found out the following facts:

1. Once you changed the args.AdvancedBorderStyle.Top to
DataGridViewAdvancedCellBorderStyle.None in the OnCellPainting method, the
AdvancedBorderStyle.Top of the DataGridView is changed to
DataGridViewAdvancedCellBorderStyle.None too.

2. Once the AdvancedBorderStyle.Top of the DataGridView has been changed to
a different value from previous, the CellPainting event of the DataGridView
will be raised automatically. Thus the OnCellPainting method is called. All
cells in the DataGridView will be in the same border style at last.

These two facts explain why the problem occurs.

I have tried modifying the statement
"args.AdvancedBorderStyle.Top=AdvancedCellBorderStyle.Top" to
the "args.AdvancedBorderStyle.Top =
DataGridViewAdvancedCellBorderStyle.Inset". When running, the result turned
out to be correct--only those cells having the same value hide their top
borders. However, the DataGridView seems to be dithering. The
OnCellPainting method is being called again an again and this calling seems
to never stop. So this modification is not good.

It seems that we have to do this on other way. I will go on researching
this issue. I appreciate your patience.



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.
====================================================
 
J

Jeffrey Tan[MSFT]

Hi steve,

Have you read my reply to you in your another post? You'd better override
AdjustCellBorderStyle method to achieve this task. If you still have any
concern, please feel free to followup me in that post. Thanks!

Best regards,
Jeffrey Tan
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.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

steve

Hi Jeff

Thanks for all your efforts on this topic

I hadn't seen your final reply to my old post on this topic but have now
printed it and looking into it

I have been able to get what I want using an override onpaint event but it
only works when the border style is set to single
(see prev reply on this thread from Linda Liu)
and I would prefer to use raised or sunken

Kind regards anyway
Steve
 
S

steve

Hi Jeff

I hadn't looked at this group for a while assuming the thread was at an end

I have tried the code you attached but I get an error message at the
ValidateRect(Me.DataGridView1.Handle, Nothing) line

A call to PInvoke function
'DataGridViewPainting!DataGridViewPainting.Form1::ValidateRect' has
unbalanced the stack. This is likely because the managed PInvoke signature
does not match the unmanaged target signature. Check that the calling
convention and parameters of the PInvoke signature match the target
unmanaged signature.



Regards

Steve
 
S

steve

Hi Jeffrey

Thanks for the help on this topic

I have got it working just fine

Regards
Steve
 
J

Jeffrey Tan[MSFT]

You are welcome.

Best regards,
Jeffrey Tan
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.
==================================================
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