Issue
My Datagrid does not populate after I click a button in my ViewModel
Model Code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace Mist_Management.SubmissionStatusBySite
{
public class Table
{
[JsonProperty("Company")]
public string Company { get; set; }
[JsonProperty("DATE")]
public string Date { get; set; }
[JsonProperty("EODNUMBER")]
public string EODNumber { get; set; }
[JsonProperty("SUBSTATUS")]
public string SubStatus { get; set; }
[JsonProperty("DAYENDSTATUS")]
public string DayEndStatus { get; set; }
}
public class BySite
{
public List<Table> Table { get; set; }
}
}
ViewModel Code:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Mist_Management.CompanyModels;
using Mist_Management.SubmissionStatusBySite;
using System;
using System.Diagnostics;
namespace Mist_Management.ViewModels
{
class SubmissionStatusBySiteViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public ObservableCollection<CompanyModels.Table> Company { get; set; }
public ObservableCollection<SubmissionStatusBySite.Table> BySite { get; set; }
public SubmissionStatusBySiteViewModel()
{
Company = new ObservableCollection<CompanyModels.Table>();
GetCompany();
}
private async void GetCompany()
{
string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/CMP/M@H$@203@R";
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(requestUrl);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string content = await response.Content.ReadAsStringAsync();
Company root = JsonConvert.DeserializeObject<Company>(content);
List<CompanyModels.Table> dates = root.Table;
foreach (var item in dates)
{
Company.Add(item);
}
}
}
}
public CompanyModels.Table selectedCompany;
public CompanyModels.Table SelectedCompany
{
get { return selectedCompany; }
set
{
selectedCompany = value;
OnPropertyChanged("SelectedCompany");
Debug.WriteLine(value.Company);
}
}
public DateTime _selectedFromDate;
public DateTime SelectedFromDate
{
get { return _selectedFromDate; }
set
{
if (_selectedFromDate == value)
return;
_selectedFromDate = value;
Debug.WriteLine(_selectedFromDate);
}
}
public string FromDate
{
get
{
return this.SelectedFromDate.ToString("yyyy-MM-dd");
}
}
public DateTime _selectedEndDate;
public DateTime SelectedEndDate
{
get { return _selectedEndDate; }
set
{
if (_selectedEndDate == value)
return;
_selectedEndDate = value;
Debug.WriteLine(_selectedEndDate);
}
}
public string EndDate
{
get
{
return this.SelectedEndDate.ToString("yyyy-MM-dd");
}
}
private async void GetData()
{
string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/SUB2/M@H$@203@R/"
+ selectedCompany.Company + "/" + FromDate + "/" + EndDate;
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(requestUrl);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string content = await response.Content.ReadAsStringAsync();
BySite root = JsonConvert.DeserializeObject<BySite>(content);
List<SubmissionStatusBySite.Table> dates = root.Table;
foreach (var item in dates)
{
BySite.Add(item);
}
}
}
Debug.WriteLine(requestUrl);
}
public ICommand LoadButton_Clicked
{
get
{
return new Command(() =>
{
BySite = new ObservableCollection<SubmissionStatusBySite.Table>();
GetData();
});
}
}
}
}
XAML code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
xmlns:system="clr-namespace:System;assembly=mscorlib"
x:Class="Mist_Management.Views.SubmissionStatusBySite"
Title="Submission Status By Site">
<StackLayout>
<Picker Title="Company List" ItemsSource="{Binding Company}"
ItemDisplayBinding="{Binding Company}"
SelectedItem="{Binding SelectedCompany}"/>
<Label Text="From Date"
FontSize="Medium"/>
<DatePicker HorizontalOptions="CenterAndExpand"
Format="yyyy-MM-dd"
MinimumDate="2015-01-01"
Date="{Binding SelectedFromDate}"/>
<Label Text="To Date"
FontSize="Medium"/>
<DatePicker HorizontalOptions="CenterAndExpand"
x:Name="EndDate"
Format="yyyy-MM-dd"
MinimumDate="2015-01-01"
Date="{Binding SelectedEndDate}"/>
<Button Text="Load" Command="{Binding LoadButton_Clicked}"/>
<ContentView>
<ScrollView Orientation="Horizontal">
<dg:DataGrid x:Name="BySiteGrid" ItemsSource="{Binding BySite}" RowHeight="70" HeaderHeight="50"
BorderColor="#CCCCCC" HeaderBackground="#E0E6F8">
<x:Arguments>
<ListViewCachingStrategy>RetainElement</ListViewCachingStrategy>
</x:Arguments>
<dg:DataGrid.HeaderFontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Tablet>15</OnIdiom.Tablet>
<OnIdiom.Phone>12</OnIdiom.Phone>
</OnIdiom>
</dg:DataGrid.HeaderFontSize>
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Company" PropertyName="Company" Width="200"/>
<dg:DataGridColumn Title="Date" PropertyName="Date" Width="150"/>
<dg:DataGridColumn Title="EOD Number" PropertyName="EODNumber" Width="150"/>
<dg:DataGridColumn Title="Sub Status" PropertyName="SubStatus" Width="150"/>
<dg:DataGridColumn Title="Day End Status" PropertyName="DayEndStatus" Width="150"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
</ScrollView>
</ContentView>
</StackLayout>
</ContentPage>
ContentView Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Mist_Management.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SubmissionStatusBySite : ContentPage
{
public SubmissionStatusBySite()
{
InitializeComponent();
BindingContext = new ViewModels.SubmissionStatusBySiteViewModel();
}
}
}
I have other content viewModels that load on screen create but when I click a button the Datagrid does not want to load. I have tried everything I can think off. I'm at my end with this problem. Any help would be appreciated
Solution
Thanks to Jason I had to set a HeightRequest on my datagrid
Answered By - Fourie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.