I’m using Linq to SQL for Database operations in C# Windows Form application. I’m trying to update ListView data when User makes any update on the database. I’ve tried listView.BeginUpdate(), listView.Refresh(), listView.EndUpdate() methods to get updates but ListView updated data now showing in ListView. When I restart the application then it shows that data. I tried debug, Database gets update before I call refreshListView() but Linq query has older data. Why Linq query showing older data?
Here is the code.
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();
Copied from comments to clarify answer:
Does
context.StuBasicsupdated from database when method is called? you need to make sure data in this variable is updated.If
context.StuBasicsis only populated when application is loaded then issue is here. You need to make sure to call it’s update every time you perform actions with DB. As LINQ in your sample is perfectly fine and alays retrieves latest data fromcontext.StuBasics