Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

all i want is to generate an ID to my listview per row it is possible?
for example i have 10 records in listview and i want to add an ID per row in randomly...



please help..


thanks in advance
Posted
Updated 8-Sep-11 19:21pm
v2
Comments
Pradeep Shukla 9-Sep-11 1:18am    
There is no picture posted as you have mentioned in the question...

There are a few ways you can generate a unique ID:

1-Use random() function to generate random number which you can use as an ID.
2-Use System.GUID.NewGUID() to generate GUID and use it. It would be an alphanumeric value though.
3-You can also use database tables to generate for you if you make it an IDENTITY column and return that value to be used as ID
4-Write your own custom logic...

The solution for your case should be like this:

Private randomClass as new Random

For i = 0 To CInt(10) - 1
strId &= Mid(strCodes, Int(Len(strCodes) * randomClass.Next() ) + 1, 1)
Next

For i = 0 To CInt(4) - 1
strId2 &= Mid(strCodes, Int(Len(strCodes) * randomClass.Next() ) + 1, 1)
Next


Rather than using Randomize() & Rnd(), I am using here the Random class and its method Next() to generate a new random number each time. You can also provide min and max value of your random number as parameter while calling Next() method.


Hope this would give you some idea to make a decision what you want to do.
 
Share this answer
 
v2
prdshukla, Thanks for your opinion but i have already construct a logic structure... but my problem is for each row in my subitem column 3 each of them have the same ID, i want is a random id for each row. I am newbie in VB.net and i don't know how to manipulate for this kind of problem...


here's my sample code


<pre lang="vb">

  Public Const A As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Public Const B As String = "0123456789"

        lstLicense.Items.Clear()
        lstLicense.Enabled = True

        Dim strCodes As String
        Dim strId As String
        Dim strId2 As String
        Dim zz As String
        Dim item As ListViewItem
        Dim random As New Random
        strCodes = strCodes & A
        strCodes = strCodes & B

        Randomize()

        For i = 0 To CInt(10) - 1
            strId &= Mid(strCodes, Int(Len(strCodes) * Rnd()) + 1, 1)
        Next

        For i = 0 To CInt(4) - 1
            strId2 &= Mid(strCodes, Int(Len(strCodes) * Rnd()) + 1, 1)
        Next

        zz = strId & "-" & strId2


        txtPartNumber.Text = zz


        For d As Integer = 0 To DataGridView1.Rows.Count - 2


            With item

                item = lstLicense.Items.Add(DataGridView1.Item(1, d).Value.ToString, 0)
                item.SubItems.Add(DataGridView1.Item(3, d).Value.ToString)2
                item.SubItems.Add(zz)

            End With
        Next
 
Share this answer
 
v2
Comments
Pradeep Shukla 9-Sep-11 1:59am    
I have updated the solution and post you responses as comments rather than as a separate solution.
thanks for your reply..

but i got an error


'Arithmetic operation resulted in an overflow.'
Private randomClass as new Random

For i = 0 To CInt(10) - 1
strId &= Mid(strCodes, Int(Len(strCodes) * randomClass.Next() ) + 1, 1)
Next
 
For i = 0 To CInt(4) - 1
strId2 &= Mid(strCodes, Int(Len(strCodes) * randomClass.Next() ) + 1, 1)
Next
 
Share this answer
 
for now i want to use "For i As Integer = 1 To 8" but i don't know how to combine with "
VB
strId2 += AZ(randomClass.Next(0, A.Length - 1)).ToString()
With item


                  item = lstLicense.Items.Add(DataGridView1.Item(2, io).Value.ToString, 0)
                  item.SubItems.Add(DataGridView1.Item(3, io).Value.ToString)
                  item.SubItems.Add(strId2)

              End With


bcoz' i want to generate an id for each row that have already record in listview

anyone have an idea?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900