Numbering RowHeader lines of a DataGridView
Anonymous user
-
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.
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.
Related links:
- [VB.NET] Select entire line ListView
- Retrieve the result of a SELECT (VB.net)
- The provider 'Microsoft.ACE.OLEDB.12.0' is not registered.
- Éteindre le PC avec VB.NET peut être effectué en utilisant la commande suivante : ```vb Process.Start("shutdown", "/s /t 0") ```
- [VB.NET] Creating a PictureBox Collection
- Retrieve the result of a select in a datagridview
3 answers
-
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 -
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 -
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.