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.

Brijesh Patel
  • 2,901
  • 15
  • 50
  • 73

2 Answers2

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

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
  • 1
    ! very nice? How can I change RowHeader Backcolor to the one you currently have.? – kashif Mar 21 '13 at 10:16
  • 2
    @kashif Or you could do with the help of: _dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue; _dataGridView.EnableHeadersVisualStyles = false; – Vishal Suthar Mar 21 '13 at 10:27
  • 1
    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 how – kashif Mar 23 '13 at 08:35
  • You can convert the code from C# to VB.NET from this website: https://converter.telerik.com/ – Codename K Feb 10 '20 at 16:18
-1

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

copy code for enter image description here

  • 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 it – KevenDenen Aug 11 '21 at 20:47