vb.net 将项目从数据库添加到 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29463404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:57:47  来源:igfitidea点击:

Add Items to a ListView from a database

databasevb.netlistviewcomboboxadd

提问by VB_nooby

I am a Vb nooby and I have trouble to add specific Items to my Listview from a database.

我是一个 Vb nooby,我无法从数据库向我的 Listview 添加特定项目。

I would like to compare the value of a combobox with a column value of a table. To proof if they are equal like apple = apple When they are equal the whole data set should be added to my ListView. (Only data sets which have the equal value like the selected item of the combobox)

我想将组合框的值与表的列值进行比较。为了证明它们是否相等,如 apple = apple 当它们相等时,应将整个数据集添加到我的 ListView 中。(仅与组合框的所选项目具有相同值的数据集)

Please Help !!

请帮忙 !!

Thanks a lot and best regards

非常感谢和最好的问候

回答by Pradnya Bolli

You can try below code..

你可以试试下面的代码..

Imports System.Data.SqlClient
Public Class Form1
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListView1.View = View.Details
Me.ListView1.GridLines = True
conn = New SqlConnection("Data Source=SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=id;Password=pass")
Dim strQ As String = String.Empty
strQ = "SELECT * FROM Northwind.dbo.Products"
cmd = New SqlCommand(strQ, conn)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "Table")
Dim i As Integer = 0
Dim j As Integer = 0
' adding the columns in ListView
For i = 0 To ds.Tables(0).Columns.Count - 1
Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
Next
'Now adding the Items in Listview
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
Next
Dim lvi As New ListViewItem(itemcoll)
Me.ListView1.Items.Add(lvi)
Next
End Sub
End Class

You can try this link.

你可以试试这个链接。

回答by VB_nooby

Thanks for your help. In my solution, I just set a parameter into the sql statement.

谢谢你的帮助。在我的解决方案中,我只是在 sql 语句中设置了一个参数。

Public Function getRahmenvertrag**(ByVal costumerID As Integer)** As List(Of Rahmenvertrag)


    Dim sqlCom As New SqlServerCe.SqlCeCommand
    sqlCom.CommandText = **"SELECT * FROM Rahmenvertrag LEFT OUTER JOIN Kunde ON Kunden_FID = Kunden_ID WHERE Kunden_ID = @Kunde "**
    **sqlCom.Parameters.AddWithValue("Kunde", costumerID)**

Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    ListView4.DataBindings.Clear()
    ListView4.Items.Clear()

    If IsNothing(ComboBox1.SelectedItem) = False Then


        For Each Rahmenvertrag As Rahmenvertrag In controller.getRahmenvertrag(ComboBox1.SelectedItem.kunde_ID)

            With ListView4.Items.Add(Rahmenvertrag.bezeichnung)
                .SubItems.Add(Rahmenvertrag.inhalt)
            End With
        Next
    End If
End Sub