Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > New to VB.NET - How can I add line numbers to DataGridView rows using VB.NET?
 

New to VB.NET - How can I add line numbers to DataGridView rows using VB.NET?

I attempted to use this code posted out on the Internet but it said that "CellStyle" was not a member of the "DataGridViewRowPostPaintEventArgs" Class. It looked great but than no cigar when I even discovered that "CellStyle" was not a member on Microsofts site.I've attempted to try and find the classes that would allow me to code this myself but am overwhelmed by the tons of classes available. I am a Beginning VB.NET programmer. Any assistance would be appreciated

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, _
  ByVal e As DataGridViewRowPostPaintEventArgs) _
    Handles dataGridView1.RowPostPaint
  ' Paint the row number on the row header.
  ' The using statement automatically disposes the brush.
  Using b As SolidBrush = _
    New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
  e.Graphics.DrawString( _
    e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), _
    e.CellStyle.Font, _
    b, _
    e.RowBounds.Location.X + 20, _
    e.RowBounds.Location.Y + 4)
End Using
End Sub
Thank you,
Wallace
wallace.davis@bankofamerica.com
nbk5533  Thursday, April 26, 2007 5:13 PM

Hi,you can add row index to the row header as this, add this code to your application will realize:

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles dataGridView1.RowPostPaint

Using b As SolidBrush = New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)

End Using

End Sub

Regards.

Gavin Jin - MSFT  Friday, April 27, 2007 1:38 AM
If you want to add row numbers to each row header cell, try this:


New
,Courier,Monospace">New,Courier,Monospace">Public Sub AddDgvRowHeaderCell(ByVal DGV As DataGridView)
New,Courier,Monospace">New,Courier,Monospace">'adds numbers for each row header cell
Dim row As DataGridViewRow
Dim i As Integer = 0
For Each row In DGV.Rows
DGV.Rows(i).HeaderCell.Value = (1 + i).ToString
i += 1
Next
End Sub



Buster952  Thursday, April 26, 2007 6:58 PM
Couple of questions. I'm not sure how to enter the correct syntax of the Font data you have suggested in vb.net. Second, do I need to define an unbound column and a way to write the values to each row?
nbk5533  Thursday, April 26, 2007 8:35 PM

Hi,you can add row index to the row header as this, add this code to your application will realize:

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles dataGridView1.RowPostPaint

Using b As SolidBrush = New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)

End Using

End Sub

Regards.

Gavin Jin - MSFT  Friday, April 27, 2007 1:38 AM

Thank you Gavin,

It appeared that the".CellStyle" property was actually ".DefaultCellStyle...". Your solution worked great. I played around, made some tweaks and it works perfect. You saved me countless hours and again appreciate your help.

Wally

nbk5533  Monday, April 30, 2007 9:30 PM

Absoutely Perfect execution! Worked perfectly for my App.

Thanks 100%

Aubynsoft  Thursday, October 25, 2007 10:08 PM

Thank U, U and your codingare great.

Sir, I am sure u can help me!,

i am new in asp.net and i am having verytrouble while i am just hosting a test site over internet host. the problem is that i am not able to execute a dynamic link library with my web application due to some security error. How i can do so ?.

do u have answer for this....

Thank U.

Ranjit Kumar Singh  Monday, February 18, 2008 1:14 PM

Hello,

When I run the following code, I see row numbering starting from 0. How do I code it so that it starts from 1?

Using b As SolidBrush = New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)

End Using

End Sub

Thanks in advance.

Rashar  Friday, June 06, 2008 1:32 PM

Hello

Remember that VB starts counting from 0 so you will need to add 1 to each rowindex.

Use the code below to start counting from 1

Using b As SolidBrush = New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)

End Using

End Sub

Hope this helps.

Aubynsoft  Friday, June 06, 2008 1:44 PM

What exactly is the error message that you get? can you copy and paste it here?

Aubynsoft  Friday, June 06, 2008 1:45 PM
Ahhhhh. Thanks.

Rashar  Friday, June 06, 2008 2:09 PM

You can use google to search for other answers

Custom Search

More Threads

• How to create custom bindingnavigator delete button?
• Making DataGridview Virtual Mode caching sample faster
• Column ValueType
• DataGridView - forcing a repaint in 1 row only
• how to export data into excel sheet????????
• Set default sort column in datagridview
• Event to change color of a datagridview row
• Two datatables in one?
• Visual Basic 2005, Visual Studio 2005, Access database combobox event?
• Datagridview - Cells ErrorText clearing