Solved

Vb.Net - Adding Rows Numbers to DataGrid

Posted on 2013-11-15
3
2,330 Views
Last Modified: 2013-11-18
Good Day Experts!

I am interested in adding row number to my Vb.Net DataGrid.  I don't want to add a column in the data.  I would like to have it in the left-hand column where the row "Indicator" is.

Is this possible? Do you have a reference that can help me achieve this?

Thanks,
jimbo9999
0
Comment
Question by:Jimbo99999
3 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 400 total points
ID: 39652707
You can do this with the HeaderCell property of the DataGridView :
The following code does it on a button click but you could do this on page_load as well:

' Set row labels. 
Private Sub Button6_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button6.Click

    Dim rowNumber As Integer = 1
    For Each row As DataGridViewRow In dataGridView.Rows
        If row.IsNewRow Then Continue For
        row.HeaderCell.Value = "Row " & rowNumber
        rowNumber = rowNumber + 1
    Next
    dataGridView.AutoResizeRowHeadersWidth( _
        DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub

Open in new window


MSDN DataGridViewRow.HeaderCell Property
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 100 total points
ID: 39655771
hi..

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
0
 

Author Closing Comment

by:Jimbo99999
ID: 39656328
Thanks all for replying...it is working now!

jimbo99999
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now