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
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