LISTVIEW每行的随机ID [英] LISTVIEW random id for each row

查看:69
本文介绍了LISTVIEW每行的随机ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我只想为每行生成一个ID到我的列表视图中?
例如,我在listview中有10条记录,我想随机添加每行的ID ...



请帮忙..


在此先感谢

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

推荐答案

有几种方法可以生成唯一的ID:

1-使用random()函数生成可以用作ID的随机数.
2-使用System.GUID.NewGUID()生成并使用GUID.不过,这将是一个字母数字值.
3-如果将其设为IDENTITY列并返回该值以用作ID
,则还可以使用数据库表为您生成 4-编写自己的自定义逻辑...

您的案例的解决方案应如下所示:

私有randomClass作为新的Random

对于i = 0到CInt(10)-1
strId& = Mid(strCodes,Int(Len(strCodes)* randomClass.Next())+ 1,1)
下一个

对于i = 0到CInt(4)-1
strId2& = Mid(strCodes,Int(Len(strCodes)* randomClass.Next())+ 1,1)
下一个


而不是使用Randomize()和Rnd(),而是在这里使用Random类及其方法Next()每次生成一个新的随机数.您还可以在调用Next()方法时提供随机数的最小值和最大值作为参数.


希望这会给您一些想法来决定您要做什么.
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.


prdshukla,谢谢您的意见,但我已经构建了一个逻辑结构...但是我的问题是我的子项目第3列中的每一行都有相同的ID,我想要的是每一行的随机ID.我是VB.net的新手,我不知道该如何解决此类问题...


这是我的示例代码


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


感谢您的回复.

但是我遇到了一个错误


``算术运算导致溢出.''
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


这篇关于LISTVIEW每行的随机ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆