473,473 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Row number in listview

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
4 6976
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: VM | last post by:
How can I fill up a listview with text file contents? My listview has two columns and the first column fills up with a while loop: while (myString != null) { myString = sr.Readline();...
0
by: keith | last post by:
In a ListView control (two columns), I added a few ListView items. ListView listview=new ListView(); listview.Parent=this; listview.View=View.Details; listview.Columns.Add...
1
by: | last post by:
How can I know what row number I clicked on in a listview? I need a method that returns an int corresponding to the row number I clicked on in my listview. Thanks.
7
by: BobAchgill | last post by:
I am trying to decide which of these controls to use to implement letting my user select a full row from MyList. The MyList has several columns which would be nice to sort by at run time. The...
2
by: Ben H | last post by:
Hello all I'm using a listview in my app but I want to scroll the listview programatically as I hate the look of the standard scrollbar. So, the listview is scrolled programatically using...
5
by: John Devlon | last post by:
Hi, Does anyone know how to get a value of a second column of a selected item in Listview. I've create a listview and added this code Listview.Items.Clear() Listview.Columns.Clear()...
1
tbarto
by: tbarto | last post by:
Hi For some time I've been working on an application that may be able to find a specified item in ListView ( WinAPI ). It is possible to get some item using e.g LVM_GETITEM message and LVITEM...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
4
by: Brian Gaze | last post by:
I have created a ListView control and have bound this to a datasource. Within the ItemTemplate of the ListView I have added another ListViewControl which is databound in the code behind. The idea...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.