473,836 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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

Cor

"steve" <ga*****@newsgr oups.nospam> schreef in bericht
news:eN******** ******@TK2MSFTN GP02.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

.AdvancedCellBo rderStyle.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

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.AdvancedBo rderStyle.Top to
DataGridViewAdv ancedCellBorder Style.None in the OnCellPainting method, the
AdvancedBorderS tyle.Top of the DataGridView is changed to
DataGridViewAdv ancedCellBorder Style.None too.

2. Once the AdvancedBorderS tyle.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.AdvancedB orderStyle.Top= AdvancedCellBor derStyle.Top" to
the "args.AdvancedB orderStyle.Top =
DataGridViewAdv ancedCellBorder Style.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
AdjustCellBorde rStyle 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.m icrosoft.com> wrote in message
news:yu******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hi steve,

Have you read my reply to you in your another post? You'd better override
AdjustCellBorde rStyle 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
'DataGridViewPa inting!DataGrid ViewPainting.Fo rm1::ValidateRe ct' 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.m icrosoft.com> wrote in message
news:ij******** ******@TK2MSFTN GXA01.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.Ad vancedCellBorde rStyle property in CellPainting event will
generate another WM_PAINT message and send to DataGridView, which will
force another painting in DataGridView(ag ain 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.m icrosoft.comwro te in message
news:J9******** ******@TK2MSFTN GXA01.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
AdjustCellBorde rStyle method in DataGridViewCel l 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
2121
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) // cell A myTable.row(0).insertCell(1) // cell B myTable.row(0).insertCell(2) // cell C What I want to see is the cells visible at the html in the order of :
1
2384
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 trying to convince mgt. to do something else). The table's width is "100%" in CSS; the top two rows are "50%" and "50%" respectively, the border is "2px solid blue", and the padding is set to "0.4em". I've also added the table attribute...
0
980
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 = e.Item.Cells(0).Text.ToLower() If value = "true" e.Item.Cells(0).Text = "Yes" Else e.Item.Cells(0).Text = "No" End If
2
19152
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 user clicks in the cell Regards
10
45220
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 event..., to no avail Any ideas Regards
5
57929
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 my code, at runtime to "Number of bikes".
2
2990
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 = Drawing.Color.Red Any suggestions? -TC
0
15254
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 should have a property of type DataGridViewAdvancedBorderStyle, this property you will change from the outside: 2. You will override also the PaintBorder, and Paint methods in a way that they will get the new property instead of the default one....
3
4159
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
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9374
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3115
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.