Row number in listview

G

Guest

Hi,

I have a listview with many rows init. The first column is "No." which shows
the row number in the listview.
When a user selected a row and delete it, say row 3.
I want to organise the number again. eg. 1,2,4,5. to 1,2,3,4. so the
listview always show the correct row number.

what is the best way to do it? many thanks.
 
G

Ged Mead

This code will renumber your first column from 1 to the end of the listview:
Dim i As Integer

For i = 0 To LVNumbered.Items.Count - 1

LVNumbered.Items(i).Text = i + 1.ToString

Next
 
G

Ged Mead

Following up my reply, I'm not sure now if you also wanted code to do the
removal of rows (?)

If so, heres' one way of doing it:

Private Sub LVNumbered_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles LVNumbered.SelectedIndexChanged

If LVNumbered.SelectedIndices.Count > 0 Then
Dim lvi As ListViewItem = LVNumbered.SelectedItems(0)
LVNumbered.Items.Remove(lvi)
End If

Dim i As Integer
For i = 0 To LVNumbered.Items.Count - 1
LVNumbered.Items(i).Text = i + 1.ToString
Next
End Sub

HTH

Ged Mead
 
C

Cor Ligthert

Jonathan,

I saw I had no nice sample of a listview, so I made it this morning

\\\Sample needs only a form the rest is made dynamicly
Friend WithEvents bt As New Button
Friend WithEvents lv As New ListView
Friend WithEvents ch1 As New ColumnHeader
Friend WithEvents ch2 As New ColumnHeader
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim lvi1 As ListViewItem = New ListViewItem(New String() {"1", "One"}, -1)
Dim lvi2 As ListViewItem = New ListViewItem(New String() {"2", "Two"}, -1)
Dim lvi3 As ListViewItem = New ListViewItem(New String() {"3",
"Three"}, -1)
Dim lvi4 As ListViewItem = New ListViewItem(New String() {"4",
"Four"}, -1)
Dim lvi5 As ListViewItem = New ListViewItem(New String() {"5",
"Five"}, -1)
lv.Columns.AddRange(New ColumnHeader() {ch1, ch2})
lv.Items.AddRange(New ListViewItem() {lvi1, lvi2, lvi3, lvi4, lvi5})
lv.Location = New System.Drawing.Point(10, 10)
lv.Name = "ListView1"
lv.Size = New System.Drawing.Size(150, 150)
lv.TabIndex = 0
lv.View = View.Details
bt.Location = New System.Drawing.Point(10, 200)
bt.Text = "Click Me"
Controls.Add(lv)
Controls.Add(bt)
End Sub
Private Sub bt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bt.Click
lv.Items.RemoveAt(2)
For i As Integer = 2 To lv.Items.Count - 1
lv.Items(i).Text = CStr(CInt(lv.Items(i).Text) - 1)
Next
End Sub
///
I hope this helps a little bit?

Cor

"Jonathan"
 
G

Guest

Big Thanks! I did the removal of selected item on list view already, but
thanks both of you anyway!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top