Numbering RowHeader lines of a DataGridView

Anonymous user -  
 Anonymous user -
Hello!
I'm giving you the trick to number the lines of a DataGridView in VB 2008.

HeaderCell.Value must be necessarily text, so we will transform the variable I from "Integer" to "String": CStr(...)

Dim I As Integer

DataGridView1.RowHeadersVisible = True
DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders

For I = 0 To DataGridView1.RowCount - 1
      If DataGridView1.Rows(I).IsNewRow = True Then Exit For
      DataGridView1.Rows(I).HeaderCell.Value = CStr(I + 1)
      DataGridView1.Rows(I).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
Next

I've used CStr(I + 1) so that it starts at 1 and not at zero. :)
If you put "DataGridView1.Rows(I).HeaderCell.Value = I + 1" you won't see anything.
There you go. I hope I've helped you.

3 answers

  1. Kalissi Posted messages 221 Status Member 20
     
    Hello,

    Note that the instruction "Cstr" is an instruction that comes from VB6

    Due to compatibility, .Net recognizes the instruction.

    Proposal:
    It would be advisable to use a VB.Net instruction

    Convert.ToString(I + 1)

    K
    0
  2. Kalissi Posted messages 221 Status Member 20
     


    The same applies to the declaration:

    Dim I as Integer

    If you type the statement:

    Convert.

    You will see that the word Integer is not there, it's Int32

    just like

    Dim I As Long

    Convert.ToInt64

    You will not find

    Convert.ToLong

    K
    0
  3. Anonymous user
     
    Re: For proper project accounting with VB 2008 or VB 2012, do as follows:
    Dim I As Int32
    DataGridView1.Columns.Add("Test", "Test")
    DataGridView1.RowHeadersVisible = True
    DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
    For I = 0 To 10
    DataGridView1.Rows.Add()
    Next
    For I = 0 To DataGridView1.RowCount - 1
    If DataGridView1.Rows(I).IsNewRow = True Then Exit For
    DataGridView1.Rows(I).HeaderCell.Value = Convert.ToString(I + 1)
    DataGridView1.Rows(I).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
    Next

    In summary, Int32 replaces Integer and Convert.ToString replaces CStr.
    0