.NET列表视图刷新 [英] .NET Listview Refresh
本文介绍了.NET列表视图刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code,基本上是从数据库中取值并填充一个列表视图。
使用(IDataReader的读卡器= cmd.ExecuteReader())
{
lvwMyList.Items.Clear();
而(reader.Read())
{
ListViewItem的LVI = lvwMyList.Items.Add(阅读器[值1]的ToString());
lvi.SubItems.Add(读卡器[值2]的ToString());
}
}
这是我的问题是,这是在较短的时间间隔(每秒),并导致重复执行的项目在ListView不断消失和重新出现。是否有某种方式从刷新,直到它与更新完成停止列表视图?喜欢的东西如下:
使用(IDataReader的读卡器= cmd.ExecuteReader())
{
lvwMyList.Items.Freeze(); //停止列表视图更新
lvwMyList.Items.Clear();
而(reader.Read())
{
ListViewItem的LVI = lvwMyList.Items.Add(阅读器[值1]的ToString());
lvi.SubItems.Add(读卡器[值2]的ToString());
}
lvwMyList.Items.UnFreeze(); //刷新列表视图
}
解决方案
这样的:
尝试
{
lvwMyList.BeginUpdate();
//唧唧歪歪
}
最后
{
lvwMyList.EndUpdate();
}
请确保您调用 lvwMyList.Items.Clear()
的在的的BeginUpdate
如果要清除列表填充它之前。
I have the following code, which basically takes values from a database and populates a listview.
using (IDataReader reader = cmd.ExecuteReader())
{
lvwMyList.Items.Clear();
while (reader.Read())
{
ListViewItem lvi = lvwMyList.Items.Add(reader["Value1"].ToString());
lvi.SubItems.Add(reader["Value2"].ToString());
}
}
The problem that I have is that this is repeatedly executed at short intervals (every second) and results in the items in the listview continually disappearing and re-appearing. Is there some way to stop the listview from refreshing until it’s done with the updates? Something like below:
using (IDataReader reader = cmd.ExecuteReader())
{
lvwMyList.Items.Freeze(); // Stop the listview updating
lvwMyList.Items.Clear();
while (reader.Read())
{
ListViewItem lvi = lvwMyList.Items.Add(reader["Value1"].ToString());
lvi.SubItems.Add(reader["Value2"].ToString());
}
lvwMyList.Items.UnFreeze(); // Refresh the listview
}
解决方案
Like this:
try
{
lvwMyList.BeginUpdate();
//bla bla bla
}
finally
{
lvwMyList.EndUpdate();
}
Make sure that you invoke lvwMyList.Items.Clear()
after BeginUpdate
if you want to clear the list before filling it.
这篇关于.NET列表视图刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文