Hello there, I'm using a ListView with interaction with database but currently I'm having a problem while binding some data.
Here's my query on database :
public async Task<List<Transaction>> GetSpentByCategory() { return await _database.QueryAsync<Transaction>("SELECT Category,Amount , count(*) as Quantity FROM 'Transaction' WHERE Type = False GROUP BY Category ORDER BY Amount;"); }
And my xaml :
<ListView x:Name="listViewSpentByCategory"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Vertical"> <StackLayout Orientation="Horizontal"> <StackLayout Orientation="Vertical"> <Label Text="{Binding Category}" HorizontalOptions="Start" TextColor="#000000" /> <Label Text="{Binding Quantity}" HorizontalOptions="Start" TextColor="#000000" /> </StackLayout> <Label Text="{Binding Amount}" HorizontalOptions="EndAndExpand" TextColor="#000000" /> <Label Text="CHF" HorizontalOptions="End" TextColor="#000000" /> </StackLayout> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
So I can correctly bind Amount and Category, but not Quantity. Do you have any idea why ? Quantity is not a column on my database, that's why my query has "as Quantity".
You still need to define a Quantity
property in your model so that it could be bound by the label.
Answers
Transaction looks like this in my database.
> You have not a quantity property in this class
Yes but isnt it right using « as Quantity »? If not , how can I bind that value? For the record, I’d like to get number of transactions by category.
You still need to define a
Quantity
property in your model so that it could be bound by the label.