DataGridView - Custom Formatting

DataGridView - Custom Formatting

Post by U211cmZtYW » Thu, 05 Feb 2009 03:25:01


Using Visual Basic 2008 - Bound CustomDGV Derived from DataGridView (DGV)

I wonder if there is a way to customize the DGV so that every 10th record /
row is highlighted in a light gray or the bottom border of that row is a
bolder line point size?

1) Is there a way to accomplish this?

2) Where can I accomplish this?
a) When the ListChanges in the Binding Source
b) When the DGV is validated
c) In a CustomDGV

Thanks
J
 
 
 

DataGridView - Custom Formatting

Post by U211cmZtYW » Thu, 05 Feb 2009 03:28:02

Using Visual Basic 2008 - Bound CustomDGV Derived from DataGridView (DGV)

I wonder if there is a way to customize the DGV so that every 10th record /
row is highlighted in a light gray or the bottom border of that row is a
bolder line point size?

1) Is there a way to accomplish this?

2) Where can I accomplish this?
a) When the ListChanges in the Binding Source
b) When the DGV is validated
c) In a CustomDGV

Thanks
J

 
 
 

DataGridView - Custom Formatting

Post by v-zhy » Thu, 05 Feb 2009 12:26:02

Hi J,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

You can handle the CellFormatting event and specify a background color for
the every 10th row. For example:

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e
As DataGridViewCellFormattingEventArgs)
If ((e.RowIndex Mod 10) _
= 0) Then
e.CellStyle.BackColor = Color.LightGray
End If
End Sub

Please try my suggestion and let me know if it make sense to you.

Have a nice day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
XXXX@XXXXX.COM .

==================================================
Get notification to my posts through email? Please refer to
http://www.yqcomputer.com/ #notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://www.yqcomputer.com/
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
 
 

DataGridView - Custom Formatting

Post by U211cmZtYW » Wed, 11 Feb 2009 23:29:01

Zhi-Xin Ye

That worked perfect, I have to add some logic to exclude row 0, and I will
look into a border instead of highlighting the whole row, drawing a border at
the bottom of the 10th row.

This is nice.
If you have any further suggestion to what I stated above that would be
great, otherwise I am good on this issue and it can be closed.

J
 
 
 

DataGridView - Custom Formatting

Post by U211cmZtYW » Wed, 11 Feb 2009 23:29:01

Zhi-Xin Ye

That worked perfect, I have to add some logic to exclude row 0, and I will
look into a border instead of highlighting the whole row, drawing a border at
the bottom of the 10th row.

This is nice.
If you have any further suggestion to what I stated above that would be
great, otherwise I am good on this issue and it can be closed.

J
 
 
 

DataGridView - Custom Formatting

Post by U211cmZtYW » Wed, 11 Feb 2009 23:29:03

Zhi-Xin Ye

That worked perfect, I have to add some logic to exclude row 0, and I will
look into a border instead of highlighting the whole row, drawing a border at
the bottom of the 10th row.

This is nice.
If you have any further suggestion to what I stated above that would be
great, otherwise I am good on this issue and it can be closed.

J
 
 
 

DataGridView - Custom Formatting

Post by v-zhy » Thu, 12 Feb 2009 12:27:29

Hi J,

Thanks for your feedback!

To paint the border instead of highlighting the whole row, you should
handle the RowPostPaint event instead.

Public Class Form2

Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles MyBase.Load
Dim dt As DataTable = New DataTable
dt.Columns.Add("c1", GetType(System.Int32))
dt.Columns.Add("c2")
Dim j As Integer = 0
Do While (j < 50)
dt.Rows.Add(j, ("aaa" + j.ToString))
j = (j + 1)
Loop
Me.DataGridView1.DataSource = dt
AddHandler DataGridView1.RowPostPaint, AddressOf
Me.dataGridView1_RowPostPaint
End Sub

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, ByVal e
As DataGridViewRowPostPaintEventArgs)
If ((e.RowIndex Mod 10) _
= 0) Then
Dim p As Pen = New Pen(Color.Red)
p.Width = 5
Dim r1 As Rectangle =
Me.dataGridView1.GetCellDisplayRectangle(0, e.RowIndex, False)
Dim r2 As Rectangle =
Me.dataGridView1.GetCellDisplayRectangle((Me.dataGridView1.ColumnCount -
1), e.RowIndex, False)
Dim p1 As Point = New Point(r1.Left, r1.Bottom)
Dim p2 As Point = New Point(r2.Right, r2.Bottom)
e.Graphics.DrawLine(p, p1, p2)
End If
End Sub

End Class

If anything is unclear or you have any concerns, please do not hesitate to
let me know, I will be happy of assistance.

Have a nice day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
XXXX@XXXXX.COM .

This posting is provided "AS IS" with no warranties, and confers no rights.