VB.NET 中的 ListView (VS 2010)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15872288/
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 13:09:48  来源:igfitidea点击:

ListView in VB.NET (VS 2010)

sqlvb.netlistview

提问by nandgate

I am trying to display sql database table data in a list view in my vb application. I have used the following code

我试图在我的 vb 应用程序的列表视图中显示 sql 数据库表数据。我使用了以下代码

Public Class Form1
    Dim conn As SqlClient.SqlConnection
    Dim cmd As SqlClient.SqlCommand
    Dim da As SqlClient.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 SqlClient.SqlConnection("Data Source=AYYAGARI-PC\WINCC;Initial Catalog=anand;Integrated Security=True")
    conn.Open()
    Dim strQ As String = String.Empty
    strQ = "SELECT * FROM [anand].[dbo].[WINCC] ORDER BY [dateandtime]"
    cmd = New SqlClient.SqlCommand(strQ, conn)
    da = New SqlClient.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
    Try
        Call Timer1_Tick(sender, e)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    For Each i As ListViewItem In ListView1.SelectedItems
        ListView1.Items.Remove(i)
    Next
    Try
        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
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

The problem with the above code is that whenever i update the table data in sql (inserting or deleting data), the same doesnt get updated in list view. Kindly help me out with this.

上面代码的问题是,每当我更新 sql 中的表数据(插入或删除数据)时,列表视图中的数据都不会更新。请帮我解决这个问题。

回答by Kasnady

    Me.ListView1.View = View.Details
    Me.ListView1.GridLines = True
    conn = New SqlClient.SqlConnection("Data Source=AYYAGARI-PC\WINCC;Initial Catalog=anand;Integrated Security=True")
    conn.Open()
    Dim strQ As String = String.Empty
    strQ = "SELECT * FROM [anand].[dbo].[WINCC] ORDER BY [dateandtime]"
    cmd = New SqlClient.SqlCommand(strQ, conn)
    da = New SqlClient.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
    Try
        Call Timer1_Tick(sender, e)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

Try to add this code again to your button which made insert or update.

尝试将此代码再次添加到进行插入或更新的按钮中。

UPDATE

更新

I give you another tips, that wont make your code so long and wasted memory or capacity..

我给你另一个提示,它不会让你的代码变得如此长和浪费内存或容量。

Try to insert all the code to new sub, e.g :

尝试将所有代码插入到新子中,例如:

    private sub mycode
    msgbox("Hello World")
    end sub >> This code won't work when you debug, cause it just like a stored code, so need to be called to make it work

Private sub Form_load......  > E.g this is your form load code
call mycode  >> Called the code above
end sub >> when you debug, your form will auto called the mycode