Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vb.Net - Adding Rows Numbers to DataGrid

Posted on 2013-11-15
3
Medium Priority
?
2,961 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 1600 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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

704 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