从数据库刷新listView [英] Refresh listView from database
问题描述
我的问题可能很愚蠢.但是我想知道在向数据库中添加插入内容后如何刷新列表视图.
我有一个非常简单的代码结构,可将数据加载到listView中.加载工作正常,但是当我在dba中添加一个人时,它不会显示在列表中.
在我从另一个窗口中单击保存"并在其中填充所有数据后,将调用refreshList.该方法的要点就像名称中所说的:刷新列表.
有人可以帮我吗?
提前谢谢!
马尔滕
Hi,
My question is probably pretty stupid. But i want to know how i can refresh my listview when i''ve added a insert into my database.
I have a very simple code structure wich loads data into my listView. The loading works just fine, but when i add a person into my dba it doesn''t show in my list.
The refreshList is called after i clicked save from an other window where i fill in all the data. The point of that method is like the name says: refreshing the list.
Can somebody help me please?
Thanks in advance!
Maarten
public void refreshList()
{
String connString2 = "server=localhost;User Id=maarten.*****;password=1234;Persist Security Info=True;database=application";
MySqlConnection conn2 = new MySqlConnection(connString2);
listView1.ItemsSource = new ObservableCollection<Person>(personList);
personList.Clear();
//listView1.InvalidateProperty(ListView.ItemsSourceProperty);
//List<Person> personeelList = new List<Person>();
//List<Person> personeelList2 = new List<Person>();
using (conn2)
{
String sql2 = "SELECT * FROM personeel ORDER BY Naam;";
MySqlCommand cmd = new MySqlCommand(sql2, conn2);
conn2.Open();
MySqlDataReader reader = cmd.ExecuteReader();
StringBuilder htmlStr = new StringBuilder("");
while (reader.Read())
{
String Naam = htmlStr.Append(reader["Naam"]).ToString();
htmlStr.Clear();
String Voornaam = htmlStr.Append(reader["Voornaam"]).ToString();
htmlStr.Clear();
String Gsm = htmlStr.Append("0" + reader["GSM"]).ToString();
htmlStr.Clear();
personList.Add(new Person(Voornaam, Naam, Gsm));
}
}
//listView1.ItemsSource = null;
listView1.Items.Refresh();
}
推荐答案
Maarten,
我不知道这是否仍然是一个问题(因为该帖子现在已经将近1个月了,但是如果是这样:请尝试使用
Hi Maarten,
I do not know if this is still an issue (since the post is now nearly 1 month old, but if so: try to use
listView1.Update();
代替
instead of
listView1.Items.Refresh();
那应该可以解决问题.
问候
菲尔
That should do the trick.
Regards
Phil
这篇关于从数据库刷新listView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!