vb.net 为什么 ListView 不刷新?

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

Why is ListView not refreshing?

vb.netwinformslistview

提问by user1683987

Hi I am inserting into a table then reading everything back into a ListViewWhen I re-open the program I see the data I just saved in my ListView, but this record is not showing in my ListViewat the time of saving (although it is saved in the table).

嗨,我插入一个表格,然后读一切恢复到ListView当我重新打开这个程序我看我刚才保存在我的数据ListView,但这个纪录没有在我的显示ListView在节省的时间(虽然它被保存在桌子)。

I am using

我在用

 listView.Update() 

as well as

 listView.Refresh() 

but it does not seem to work. I am just using a SELECTquery to read data off the datareaderand store it in my listViewas follows (which is working properly)

但它似乎不起作用。我只是使用SELECT查询来读取数据datareader并将其存储在我的listView如下(工作正常)

The only problem is that the listView is not refreshing immediately, but it works when I close/open the program

唯一的问题是 listView 没有立即刷新,但是当我关闭/打开程序时它可以工作

here's the code I am using at the end of my Save method. basically these two methods get called

这是我在 Save 方法末尾使用的代码。基本上这两种方法被调用

  Private Sub SetColumns()
    Dim lstpenalty As New ListView()
    lstpenalty.Items.Clear()
    lstpenalty.Visible = True
    lstpenalty.Bounds = New Rectangle(New Point(390, 55), New Size(560, 379))
    lstpenalty.Name = "lstpenalty"
    lstpenalty.FullRowSelect = True


    lstpenalty.View = View.Details
    lstpenalty.GridLines = True


    lstpenalty.Items.Clear()


    lstpenalty.Columns.Add("a", 75)
    lstpenalty.Columns.Add("b", 70)
    lstpenalty.Columns.Add("c", 105)
    lstpenalty.Columns.Add("d", 98)
    lstpenalty.Columns.Add("e", 90)
    lstpenalty.Columns.Add("f", 105)
    Me.Controls.Add(lstpenalty)



    LoadPenaltyList(lstpenalty)

End Sub

Private Sub LoadPenaltyList(ByRef listView As ListView)

    Dim gDR As OracleDataReader
    Dim cmd As New OracleCommand
    Dim lstpenaltyview As New ListViewItem

    Try

        cnn.Connect_to_Oper_Agent()
        cmd.Connection = cnn.cnn

        listView.Items.Clear()


        cmd.CommandText = " select a," & _
                          "b, " & _
                          "c, " & _
                          "d," & _
                          "e," & _
                          "f" & _
                          " FROM myTable" & _
                          " commit"

        gDR = cmd.ExecuteReader()


        While gDR.Read

            lstpenaltyview = listView.Items.Add(Null_To_String(gDR("a")))
            lstpenaltyview.SubItems.Add(gDR("b"))
            lstpenaltyview.SubItems.Add(gDR("c"))
            lstpenaltyview.SubItems.Add(gDR("d"))
            lstpenaltyview.SubItems.Add(gDR("e"))
            lstpenaltyview.SubItems.Add(gDR("f"))

        End While

        listView.Update()



    Catch ex As Exception
        MsgBox("There was an error... -> " & ex.ToString)

    Finally
        cmd.Dispose()
        cnn.Close_Conn()

    End Try


End Sub

Thanks for your help

谢谢你的帮助

采纳答案by user1683987

So I was creating the ListView dynamically. The issue was that it kept creating ListViews over each other (memory leak), and it always showed the previous one. I created a static viewList versus a dynamic one and the issue is resolved:D

所以我正在动态创建 ListView。问题是它不断地相互创建 ListViews(内存泄漏),并且总是显示前一个。我创建了一个静态视图列表而不是动态视图列表,问题得到了解决:D

回答by stevepkr84

It is worth looking at what methods do.

值得看看方法是做什么的。

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview_methods%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview_methods%28v=vs.71%29.aspx

Update : Causes the control to redraw the invalidated regions within its client area.

更新:使控件重绘其客户区内的无效区域。

Refresh: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

刷新:强制控件使其客户区无效并立即重绘自身和任何子控件。

To update it using table info, first Clear the listview, then redo the process you used to initially populate it.

要使用表信息更新它,首先清除列表视图,然后重做最初填充它时使用的过程。

回答by gonzalo

Use

For Each i As Windows.Forms.ListViewItem In ListView1.Items
   i.Remove()
Next

Instead of Clear

代替 Clear