Add row number to datagridview in vb.net

I am developing a Windows application in Vb.Net. Now there is one form in which I want to print the entries displayed in the grid. It is possible to sort the grid by clicking on the cell header in the grid. And it should be printed as shown in the grid.

So, I'm a little confused how to maintain the line number in the grid. I can take the row number from the database initially when the grid is full and the data source is assigned. But when the user clicks on any cell header and sorts this column, the row number changes. At that time, it was very difficult for me to maintain the line number.

Can someone give me an idea on how to maintain the line number in the grid?

Thanks in advance.

+4
source share
1 answer

I think 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

+6
source

All Articles