繁体   English   中英

ListView在运行时未显示更新

[英]ListView is not showing updates at runtime

我正在使用Linq to SQL在C#Windows Form应用程序中进行数据库操作。 当用户在数据库上进行任何更新时,我正在尝试更新ListView数据。 我试过listView.BeginUpdate()listView.Refresh()listView.EndUpdate()方法来获取更新,但ListView更新后的数据现在显示在ListView中。 当我重新启动应用程序时,它将显示该数据。 我尝试调试,在调用refreshListView()之前数据库得到更新,但是Linq查询具有较旧的数据。 为什么Linq查询显示较旧的数据? 这是代码。

studentViewLv.Clear();
studentViewLv.BeginUpdate();
var query = from c in context.StuBasics select c;
studentViewLv.Columns.Add("Ser No", 50);
studentViewLv.Columns.Add("Student Name ", 200);
studentViewLv.Columns.Add("Father's Name", 150);
studentViewLv.Columns.Add("Registration No", 150);
studentViewLv.Columns.Add("Class", 100);
studentViewLv.FullRowSelect = true;
int i = 1;
foreach (var c in query)
{
    string[] stu = new string[] { i.ToString(), c.firstName + " " + c.lastName, c.fatherName, c.registrationNo, c.currentClass };
    ListViewItem item = new ListViewItem(stu);
    studentViewLv.Items.Add(item);
    i++;
}
studentViewLv.Refresh();
studentViewLv.Update();
studentViewLv.EndUpdate();    

从评论中复制以澄清答案:

调用方法时, context.StuBasics是否从数据库更新? 您需要确保此变量中的数据已更新。

如果仅在加载应用程序时填充context.StuBasics则问题在此处。 您需要确保每次对数据库执行操作时都将其称为更新。 作为LINQ的样本中是完全正常和alays检索来自最新数据context.StuBasics

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM