Add row number in datagridview in vb.net
Asked Answered
D

2

6

I am developing Windows Application in Vb.Net. Now there is one form in which I want to print records displayed in the grid. There is a facility to sort grid by clicking on the cell header in the grid And that should be print as displayed in the grid.

So I am little bit confuse about how to maintain the row number in the grid. I can take row number from DB initially when grid fill and assign data source. But when user clicks any cell header and sort that column then the row number is changed. At that time it is very difficult for me to maintain the row number.

Can any one give me idea how to maintain row number in the grid?

Thanks in advance.

Drink answered 9/1, 2013 at 7:12 Comment(4)
What Kind of Database do u use?Gabe
you don't want to change the order on sort..?Vc
@M.NourBerro We are using SQL Server 2008.Drink
@VishalSuthar no when i print the record from the grid it should print same as display in grid after sorting or not sorting.Drink
V
8

I guess you need this:

NOTE: This code is in C# so you can just convert it to VB.Net

delegate:

this.dgvUserDetails.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dgvUserDetails_RowPostPaint);

Event:

private void dgvUserDetails_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
        using (SolidBrush b = new SolidBrush(dgvUserDetails.RowHeadersDefaultCellStyle.ForeColor))
        {
              e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        }
}

OUTPUT

row number

Vc answered 9/1, 2013 at 7:20 Comment(4)
! very nice? How can I change RowHeader Backcolor to the one you currently have.?Pigfish
@Pigfish Or you could do with the help of: _dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue; _dataGridView.EnableHeadersVisualStyles = false;Vc
thanks for introducing such a good visualstyler to me. I installed it from the website you mentioned and changed my projects style using office2010 skin. now my application looks many times better than the one it was earlier. after a few days it started to ask me for licence. can you please tell me where did you download the cracked version from?? I downloaded the version v2.4.50000.1 and didn't find any key free of cost from google. it worths 250 us dollars equals to 25000 pak rupees that I can't even think of. I would be really happy if you solve my registeration problem any howPigfish
You can convert the code from C# to VB.NET from this website: converter.telerik.comLightfooted
F
-1

grid.Rows(e.RowIndex).HeaderCell.Value = CStr(e.RowIndex + 1)

copy code for enter image description here

Fabiola answered 11/8, 2021 at 4:13 Comment(1)
We don't expect every answer to be perfect, but answers with correct spelling, punctuation, and grammar are easier to read. They also tend to get upvoted more frequently. Remember, you can always go back at any time and edit your answer to improve itWraith

© 2022 - 2024 — McMap. All rights reserved.