I have an invoice table, I'm loading the data like this (and it's working fine):
private ObservableCollection<InvoiceData> _listAchats;
public ObservableCollection<InvoiceData> ListAchats
{
get { return _listAchats; }
set
{
_listAchats = value;
NotifyPropertyChanged("ListAchats");
}
}
ViewModel:
_listAchats = new ObservableCollection<InvoiceData>();
ListAchats = GetAchatList();
public ObservableCollection<InvoiceData> GetAchatList()
{
//Count = 0;
ObservableCollection<InvoiceData> ClientsList = new ObservableCollection<InvoiceData>();
using (SQLiteConnection conn =
new SQLiteConnection(ConfigurationManager.ConnectionStrings["StringConnexion"].ConnectionString))
{
conn.Open();
SQLiteCommand cmdNotes =
new SQLiteCommand("SELECT * FROM Invoice", conn);
// cmdNotes.Parameters.AddWithValue("@MAT_EMP", Matricule);
using (SQLiteDataReader reader = cmdNotes.ExecuteReader())
{
var ordinals = new
{
InvoiceId = reader.GetOrdinal("invoice_id"),
SellerName = reader.GetOrdinal("seller_name"),
PurchaseDate = reader.GetOrdinal("purchase_date")
};
while (reader.Read())
{
var temp = new TableInvoice();
temp.IdInvoice = reader.GetInt32(ordinals.InvoiceId);
temp.SellerName = reader.GetString(ordinals.SellerName);
temp.PurchaseDate = reader.GetDateTime(ordinals.PurchaseDate).ToShortDateString();
// temp.JoinDateString = RelativeTime.ConvertDate(reader.GetDateTime(ordinals.JoinDate));
ClientsList.Add(temp.ClientsList());
}
}
}
return ClientsList;
}
Now, what I want to do is that whenever I fill a certain form (insert it into a database table) is to update and refresh this ObservableCollection. I have a button doing this:
private void Refresh()
{
Console.WriteLine("This is triggered");
ListAchats.Clear();
ListAchats = GetAchatList();
Console.WriteLine(ListAchats.Count);
}
The DataGrid, goes empty... but never reload again for some reason. Any thoughts on what could the reason be? This is the XAML:
<DataGrid Margin="0 16 0 0"
Name="Agrid"
Background="Transparent"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
AutoGenerateColumns="False"
Focusable="False"
MaxHeight="400"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
CanUserSortColumns="True"
SelectedItem="{Binding SelectedAchat}"
ItemsSource="{Binding ListAchats,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CanUserAddRows="False"
IsReadOnly="True" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteButtonCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTemplateColumn SortMemberPath="IdInvoice" Header="N° Achat">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Path=IdInvoice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn SortMemberPath="SellerName" Header="Nom de vendeur" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Path=SellerName, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn SortMemberPath="PurchaseDate" Header="Date d'achat" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Path=PurchaseDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Like I previously said, the ObservableCollection works the first time I run the app, just not when I refresh it.
CodePudding user response:
From your explanation, I dare to assume that you have something wrong with the change notification.
Try this implementation:
// "ReadOnly" propery
public ObservableCollection<InvoiceData> ListAchats {get;} = new();
public IEnumerable<InvoiceData> GetAchatList()
{
List<InvoiceData> clientsList = new List<InvoiceData>();
// Some Code
// When returning a list, just in case,
// we protect the original list from changes.
return clientsList.Select(cl => cl);
}
private void Refresh()
{
Console.WriteLine("This is triggered");
ListAchats.Clear();
foreach(var cl in GetAchatList())
ListAchats.Add(cl);
Console.WriteLine(ListAchats.Count);
}
CodePudding user response:
Apparently, I forgot to add INotifyPropertyChanged
interface to my ViewModel. That's what caused the issue.