By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Row number in listview

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Jonathan" <Jo******@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
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.

Nov 21 '05 #2

P: n/a
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
"Jonathan" <Jo******@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
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.

Nov 21 '05 #3

P: n/a
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"
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.

Nov 21 '05 #4

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

"Cor Ligthert" wrote:
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"
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.


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.