简体   繁体   English

所有列表视图数据使用循环显示在文本框中

[英]All Listview data show in textbox using loop

    Dim Mysqlconn = New SqlConnection
    Mysqlconn.ConnectionString = "Data Source=DESKTOP-D32ONKB;Initial Catalog=Attendance;Integrated Security=True"

    Dim dt As DataTable = New DataTable("studentdata")

    Mysqlconn.Open()

    Dim query As String
    query = "select ID from studentdata where Class='" & ComboBox1.Text & "'"

    Dim Command = New SqlCommand(query, Mysqlconn)
    Dim dr = Command.ExecuteReader(CommandBehavior.CloseConnection)

    ListView1.Items.Clear()
    Dim x As ListViewItem

    Do While dr.Read = True
        x = New ListViewItem(dr("ID").ToString)
        ListView1.Items.Add(x)
    Loop

    For i = 0 To ListView1.Items.Count - 1
        TextBox1.Text = ListView1.Items(i).SubItems(0).Text
    Next

In this code, Textbox1 is showing the last row from Listview1 .在此代码中, Textbox1显示Listview1的最后一行。 My requirement is all the Listview1 data show in textbox1 one after one from Listview1.我的要求是Listview1中textbox1中的所有Listview1数据一一显示。 Is this possible to show in textbox1 read all data from Listview1 using loop.是否可以在 textbox1 中显示使用循环从 Listview1 读取所有数据。 Thank you...谢谢...

A textbox only holds one string at a time.一个文本框一次只包含一个字符串。 If it's set to allow multiline strings (not clear in the question) you can separate each item with a linebreak.如果它设置为允许多行字符串(问题中不清楚),您可以用换行符分隔每个项目。 Otherwise you can separate the strings using a delimiter like a comma.否则,您可以使用逗号等分隔符分隔字符串。

Dim query As String = "select ID from studentdata where Class= @class"
Using conn As New SqlConnection("Data Source=DESKTOP-D32ONKB;Initial Catalog=Attendance;Integrated Security=True"), _
      cmd  As New SqlCommand(query, conn)

    cmd.Parameters.Add("@class", SqlDbType.NVarChar, 20).Value =  ComboBox1.Text
    conn.Open()
    Using dr As SqlDataReader = cmd.ExecuteReader()
        While dr.Read()
            ListView1.Items.Add(New ListViewItems(dr("ID").ToString()))
        End While
    End Using
End Using

TextBox1.Text = String.Join(",", ListView1.Items.Select(Function(i) i.SubItems(0).Text))

Also note how I used a query parameter to include the combobox value in the SQL command.另请注意我如何使用查询参数在 SQL 命令中包含 combobox 值。 That's a big deal ;这是一件大事 anything else will give you trouble, usually sooner than later.任何其他事情都会给你带来麻烦,通常迟早会出现。

Using as loop, the proper way would be like so:使用 as 循环,正确的方法是这样的:

Dim lines As New List(Of String)

For i = 0 To ListView1.Items.Count - 1
    lines.Add(ListView1.Items(i).Text)
Next

TextBox1.Lines = lines.ToArray()

You can't keep setting the Text property to a new value and expect the old value to hang around for no reason.您不能一直将Text属性设置为新值并期望旧值无缘无故地存在。 You could append to the Text each time, but that is inefficient.您每次都可以将 append 发送到Text ,但效率很低。 The proper way is to create a list of the values, convert that to a String array and then assign that to the Lines property.正确的方法是创建一个值列表,将其转换为String数组,然后将其分配给Lines属性。

Note that there is no point getting the Text of the first subitem because that is the same as the Text of the item.请注意,获取第一个子项的Text没有意义,因为它与项目的Text相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM