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
..AdvancedCellB orderStyle.All = DataGridViewAdv ancedCellBorder Style.Inset
End With
End Sub
Protected Overrides Sub OnCellFormattin g(ByVal args As
DataGridViewCel lFormattingEven tArgs)
' Call home to base
MyBase.OnCellFo rmatting(args)
' First row always displays
If args.RowIndex = 0 Then
Return
End If
If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then
args.Value = String.Empty
args.Formatting Applied = True
End If
End Sub
Private Function IsRepeatedCellV alue(ByVal rowIndex As Integer, ByVal
colIndex As Integer) As Boolean
Dim currCell As DataGridViewCel l = Rows(rowIndex). Cells(colIndex)
Dim prevCell As DataGridViewCel l = Rows(rowIndex - 1).Cells(colInd ex)
If Not IsNothing(currC ell) AndAlso Not IsNothing(prevC ell) AndAlso Not
IsNothing(currC ell.Value) AndAlso Not IsNothing(prevC ell.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
DataGridViewCel lPaintingEventA rgs)
MyBase.OnCellPa inting(args)
' Ignore column and row headers and first row
If args.RowIndex < 1 OrElse args.ColumnInde x < 0 Then
Return
End If
If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then
args.AdvancedBo rderStyle.Top = DataGridViewAdv ancedCellBorder Style.None
Else
args.AdvancedBo rderStyle.Top = AdvancedCellBor derStyle.Top
End If
End Sub
End Class
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
..AdvancedCellB orderStyle.All = DataGridViewAdv ancedCellBorder Style.Inset
End With
End Sub
Protected Overrides Sub OnCellFormattin g(ByVal args As
DataGridViewCel lFormattingEven tArgs)
' Call home to base
MyBase.OnCellFo rmatting(args)
' First row always displays
If args.RowIndex = 0 Then
Return
End If
If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then
args.Value = String.Empty
args.Formatting Applied = True
End If
End Sub
Private Function IsRepeatedCellV alue(ByVal rowIndex As Integer, ByVal
colIndex As Integer) As Boolean
Dim currCell As DataGridViewCel l = Rows(rowIndex). Cells(colIndex)
Dim prevCell As DataGridViewCel l = Rows(rowIndex - 1).Cells(colInd ex)
If Not IsNothing(currC ell) AndAlso Not IsNothing(prevC ell) AndAlso Not
IsNothing(currC ell.Value) AndAlso Not IsNothing(prevC ell.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
DataGridViewCel lPaintingEventA rgs)
MyBase.OnCellPa inting(args)
' Ignore column and row headers and first row
If args.RowIndex < 1 OrElse args.ColumnInde x < 0 Then
Return
End If
If IsRepeatedCellV alue(args.RowIn dex, args.ColumnInde x) Then
args.AdvancedBo rderStyle.Top = DataGridViewAdv ancedCellBorder Style.None
Else
args.AdvancedBo rderStyle.Top = AdvancedCellBor derStyle.Top
End If
End Sub
End Class
Comment