How to update/ refresh Listview from SQLite Database?

Sarah 186 Reputation points
2022-04-12T07:56:19.557+00:00

I am using SQLite Database. When changing listview item and not saving(click edit button, make some changes and click cancel button), the changes are not applied in Database(Correct) but in Listview they are(Wrong). Can anyone help to solve this problem?

All the code can be found here:

public class ContactViewModel  
    {  
  
        public ICollectionView View  
        {  
            get  
            {  
                SqliteDataAccess.Instance.UpLoadData();  
                cvs.Source = SqliteDataAccess.Instance.GetData();  
                return cvs.View;  
                  
            }  
        }  
        private CollectionViewSource cvs = new CollectionViewSource();  
        public ObservableCollection<ContactDetail> ContactDetails { get; set; }  
  
        public ContactDetail CurrentContact { get; set; }  
  
  
        private AddContact addContactWindow;  
        private bool? ret;  
  
        public void CmdExecute(object parameter)  
        {  
            switch (parameter.ToString())  
            {  
                case "Add_Click":  
                    addContactWindow = new AddContact() { DataContext = this };  
                    CurrentContact = new ContactDetail();  
                    ret = addContactWindow.ShowDialog();  
  
                    if (ret.HasValue && ret.Value)  
                    {  
                        SqliteDataAccess.Instance.AddNew(CurrentContact);  
                    }  
                    else  
                    {  
                        CurrentContact = null;  
                    }  
                    break;  
  
                case "Edit_Click":         
                    if (CurrentContact  != null)  
                    {  
                        addContactWindow = new AddContact() { DataContext = this };  
                        ret = addContactWindow.ShowDialog();  
                        if (ret.HasValue && ret.Value)  
                        {  
                            SqliteDataAccess.Instance.UpDate(CurrentContact);  
                        }  
                        else  
                        {  
                            cvs.View.Refresh();  
                            OnPropertyChanged(nameof(View));  
                        }  
                    }  
                    else  
                    {  
                        return;  
                    }  
                    break;  
  
                case "Remove_Click":  
                    if (CurrentContact  != null)  
                    {  
                        SqliteDataAccess.Instance.Delete(CurrentContact);  
                    }  
                    else  
                    {  
                        return;  
                    }  
                    break;  
  
                default:  
                    break;  
            }  
        }  
  
          
        public event PropertyChangedEventHandler PropertyChanged;  
        internal void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = "") =>  
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
  
    }  
}  
  
public class SqliteDataAccess  
{  
	ObservableCollection<ContactDetail> DataList = new ObservableCollection<ContactDetail>();  
  
	internal ObservableCollection<ContactDetail> GetData()  
	{  
		DataList.Clear();  
		foreach (DataRow row in SqlDt.Rows)  
		{  
			ContactDetail instance = new ContactDetail()  
			{  
				Id = row.Field<long>("Id"),  
				Name = row.Field<string>("Name"),  
				//etc  
			};  
			DataList.Add(instance);  
		}  
		return DataList;  
	}  
  
  
	public void UpLoadData()  
	{  
		SqlConn.ConnectionString = LoadConnectionSting();  
		try  
		{  
			SqlConn.Open();  
			SqlCmd.Connection = SqlConn;  
			SqlCmd.CommandText = "SELECT * FROM " + DbTableName;  
			SqlRd = SqlCmd.ExecuteReader();  
			SqlDt.Load(SqlRd);  
		}  
		catch (Exception ex)  
		{  
			 MessageBox.Show(ex.Message);  
		}  
		finally  
		{  
			SqlRd.Close();  
			SqlConn.Close();  
		}  
	}  
}  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,694 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2022-04-13T06:55:31.57+00:00

    Hi Sarah,
    you can change your edie cas:

        case "Edit_Click":
          if (CurrentContact != null)
          {
            addContactWindow = new AddContact() { DataContext = this };
            ret = addContactWindow.ShowDialog();
            if (ret.HasValue && ret.Value) SqliteDataAccess.Instance.UpDate(CurrentContact);
            else
            {
              cvs.Source = SqliteDataAccess.Instance.GetData();
              cvs.View.Refresh();
              OnPropertyChanged(nameof(View));
            }
          }
          break;
    

0 additional answers

Sort by: Most helpful