Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add row number in datagridview in vb.net

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.

like image 573
Brijesh Patel Avatar asked Jan 09 '13 07:01

Brijesh Patel


1 Answers

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

like image 69
Vishal Suthar Avatar answered Oct 12 '22 15:10

Vishal Suthar