473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Changing datagridview cells borders at runtime

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
Jun 18 '06 #1
7 9648
steve,

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

Cor

"steve" <ga*****@newsgroups.nospam> schreef in bericht
news:eN**************@TK2MSFTNGP02.phx.gbl...
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

Jun 18 '06 #2
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=AdvancedCellBorderSt yle.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.
================================================== ==

Jun 19 '06 #3
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.

Jun 21 '06 #4
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
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> wrote in message
news:yu**************@TK2MSFTNGXA01.phx.gbl...
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.

Jun 21 '06 #5
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

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> wrote in message
news:ij**************@TK2MSFTNGXA01.phx.gbl...
Hi Steve,

Have you recieved my sample project? Does it make sense to you? If you
still need any help, please feel free to tell me.

Additionally, I found that there is some strange performance issue in my
sample project. After performing some debugging I found that changing
DataGridView.AdvancedCellBorderStyle property in CellPainting event will
generate another WM_PAINT message and send to DataGridView, which will
force another painting in DataGridView(again calls CellPainting event ),
so
the DataGridView UI will keep painting all the time and have a flicker UI.

To resolve this issue, you may place a ValidateRect Win32 calling in
CellPainting event, which removes the WM_PAINT message in the message
queue. Attached is the modified sample project, which works well now. Hope
it helps!

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.

Jun 28 '06 #6
Hi Jeffrey

Thanks for the help on this topic

I have got it working just fine

Regards
Steve
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:J9**************@TK2MSFTNGXA01.phx.gbl...
Hi Steve,

Thanks for your feedback!

Oh, yes, I original tested the project in release build which can not see
this error. By testing it under debugger, I can see this error. The error
is caused by an improper API declaration: the second parameter to
ValidateRect should be ByRef instead of ByVal. However, after correcting
this error, it seems that it does not take effect. So it seems that
ValidateRect can not remove WM_PAINT message from the message queue.

Currently, I think the only valid solution is overriding
AdjustCellBorderStyle method in DataGridViewCell and return a customized
cell. I have written a sample project to demonstrate this technology,
please see the attachment. Just click the button on the Form to see the
effect.

Hope it helps!

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.

Jul 5 '06 #7
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.

Jul 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eitan | last post by:
Hello, In a table element How can I insert cell in their order (A, B, C, D ...) on javascript runtime, but see the cells inserted from right to left. I am doing : myTable.row(0).insertCell(0) //...
1
by: Elf M. Sternberg | last post by:
I have a table of two columns and four rows. The top two rows are occupied by two cells each; the bottom two rows are set ROWSPAN=2. (Yes, I'm using this for layout. It's a legacy app. I'm...
0
by: Mythran | last post by:
I have seen many examples on the Internet that do the following to format data being bound to a DataGrid....IMO, it's sick.. Public Sub MyGrid_ItemDataBound( ... ) Dim value As String =...
2
by: steve | last post by:
Hi All How do I get the screen coordinates of the selected cell boundaries in a datagridview I need to position a form at the top left position of the selected cell regardless of where the...
10
by: steve | last post by:
Hi All I would like to be able to change the cell borders on certain cells to none at runtime to make a group of cells appear to be merged I have tried the following in the cellformatting...
5
by: Kimmo Laine | last post by:
Hi is there a way to change propertys attribute from the code? Letīs say that i have the following property in my class: public int Count } Is there a way to change the displayname, from...
2
by: TC | last post by:
I can't figure out how to change the background color for cells in a DataGridView. I've tried the obvious, but it doesn't seem to work: MyDataGridView.Item(1, 1).Style.BackColor =...
0
by: ziketo | last post by:
Hi, I searched a lot about changing the DataGridView cell borders. BYTES helped me so I will write down the solution: 1. You should override the class DataGridViewTextBoxCell, and the new class...
3
by: Parameswar Nayak | last post by:
Hi every one I am using datagridview in a win application.I need to validate the different cells according to condition like one cell for entering only numeric value. Can some one help me?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.