question

FourieLaing-1416 avatar image
0 Votes"
FourieLaing-1416 asked ·

How to get switch to save values from my datagrid

I'm using a datagrid with a switch and I want to pass certain values for each switch that is toggled to somewhere else in my ViewModel. That will be called on button click. The values I want to save is the Company name and the OrderNumber

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"
          x:Class="Mist_Management.Views.ApprovalPage"
          Title="Approvals">
 <StackLayout Padding="5">
     <ContentView>
         <ScrollView Orientation="Horizontal">
             <dg:DataGrid x:Name="ApprovalGrid" ItemsSource="{Binding MistHeaders}" SelectedItem="{Binding SelectedItem}" 
                          RowHeight="70" HeaderHeight="50" BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" 
                          HeightRequest="1000" ActiveRowColor="#8899AA">
                 <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="Tag" Width="100">
                         <dg:DataGridColumn.CellTemplate>
                             <DataTemplate>
                                 <Switch IsToggled="{Binding .}" HorizontalOptions="Center"/>
                             </DataTemplate>
                         </dg:DataGridColumn.CellTemplate>
                     </dg:DataGridColumn>
                     <dg:DataGridColumn Title="Company" PropertyName="Company" Width="200"/>
                     <dg:DataGridColumn Title="Payment Type" PropertyName="PaymentType" Width="150"/>
                     <dg:DataGridColumn Title="Received Date" PropertyName="RecievedDate" Width="150"/>
                     <dg:DataGridColumn Title="Request No" PropertyName="RequestNo" Width="150"/>
                     <dg:DataGridColumn Title="Supplier Name" PropertyName="SupplierName" Width="200"/>
                     <dg:DataGridColumn Title="Quote No" PropertyName="QuoteNo" Width="150"/>
                     <dg:DataGridColumn Title="Total" PropertyName="Total" Width="150"/>
                     <dg:DataGridColumn Title="Justification" PropertyName="Justification" Width="300"/>
                     <dg:DataGridColumn Title="Order Number" PropertyName="OrderNumber" Width="150"/>
                 </dg:DataGrid.Columns>
             </dg:DataGrid>
         </ScrollView>
     </ContentView>
     <!--<dg:DataGrid x:Name="Grid" ItemsSource="{Binding OtherDetails}"
                          RowHeight="70" HeaderHeight="50" BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" 
                          HeightRequest="1000" ActiveRowColor="#8899AA">
         <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="Attachment Name" PropertyName="AttachmentName" Width="200"/>
             <dg:DataGridColumn Title="Attachment Google View" PropertyName="AttachmentGoogleView" Width="150"/>
         </dg:DataGrid.Columns>
     </dg:DataGrid>-->
     <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
         <Button Text="Approval" Command="{Binding ApprovalButton_Clicked}"/>
         <Button Text="Reject" Command="{Binding RejectButton_Clicked}"/>
         <Button Text="To Be Corrected" Command="{Binding ToBeCorrectedButton_Clicked}"/>
     </StackLayout>
     <Button Text="Attachments" HorizontalOptions="Center" Command="{Binding AttachmentButton_Clicked}"/>
 </StackLayout>
 </ContentPage>

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.Models;
 using Mist_Management.AttachmentModel;
 using System.Diagnostics;
    
 namespace Mist_Management.ViewModels
 {
 public class ApprovalPageViewModel : INotifyPropertyChanged 
 {
     public ObservableCollection<Models.Table> MistHeaders { get; set; }
     public ObservableCollection<AttachmentModel.Table> OtherDetails { get; set; }
    
     public ApprovalPageViewModel()
     {
         MistHeaders = new ObservableCollection<Models.Table>();
         OtherDetails = new ObservableCollection<AttachmentModel.Table>();
         GetData();
     }
    
     private async void GetData()
     {
         string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/head/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();
                 MistHeaders root = JsonConvert.DeserializeObject<MistHeaders>(content);
                 List<Models.Table> dates = root.Table;
    
                 foreach (var item in dates)
                 {
                     MistHeaders.Add(item);
                 }
             }
         }
     }
    
     public Models.Table selectedItem;
     public Models.Table SelectedItem
     {
         get { return selectedItem; }
         set
         {
             selectedItem = value;
             OnPropertyChanged("SelectedItem");
             Debug.WriteLine(value.Company);
             Debug.WriteLine(value.OrderNumber);
         }
     }
    
     private async void GetAttachments()
     {
         string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/ATT/M@H$@203@R/" + selectedItem.OrderNumber +
             "/" + selectedItem.Company;
    
         using (var client = new HttpClient())
         {
             HttpResponseMessage response = await client.GetAsync(requestUrl);
             try
             {
                 if (response.StatusCode == System.Net.HttpStatusCode.OK)
                 {
                     string content = await response.Content.ReadAsStringAsync();
                     Attachment root = JsonConvert.DeserializeObject<Attachment>(content);
                     List<AttachmentModel.Table> dates = root.Table;
    
                     foreach (var item in dates)
                     {
                         OtherDetails.Add(item);
                     }
                 }
             }
             catch
             {
                 Debug.WriteLine(requestUrl);
             }
         }
     }
    
     private async void SendApprovals()
     {
         string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/AUT/M@H$@203@R/" + selectedItem.OrderNumber +
             "/" + selectedItem.Company;
         using (var client = new HttpClient())
         {
             HttpResponseMessage response = await client.GetAsync(requestUrl);
         }
     }
    
     public ICommand AttachmentButton_Clicked
     {
         get
         {
             return new Command(() =>
             {
                 GetAttachments();
             });
         }
     }
    
     public ICommand ApprovalButton_Clicked
     {
         get
         {
             return new Command(() =>
             {
    
             });
         }
     }
    
     public ICommand RejectButton_Clicked
     {
         get
         {
             return new Command(() =>
             {
    
             });
         }
     }
    
     public ICommand ToBeCorrectedButton_Clicked
     {
         get
         {
             return new Command(() =>
             {
    
             });
         }
     }
    
     public event PropertyChangedEventHandler PropertyChanged;
    
     private void OnPropertyChanged(string property)
     {
         if (PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(property));
     }
 }
 }

Model Code:

 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.Text;
    
 namespace Mist_Management.Models
 {
 public class Table
 {
     [JsonProperty("Company")]
     public string Company { get; set; }
     [JsonProperty("Payment Type")]
     public string PaymentType { get; set; }
     [JsonProperty("Recieved Date")]
     public string RecievedDate { get; set; }
     [JsonProperty("Request No.")]
     public string RequestNo { get; set; }
     [JsonProperty("Supplier Name")]
     public string SupplierName { get; set; }
     [JsonProperty("Quote No.")]
     public string QuoteNo { get; set; }
     [JsonProperty("Total")]
     public string Total { get; set; }
     [JsonProperty("Justification")]
     public string Justification { get; set; }
     [JsonProperty("Order Number")]
     public string OrderNumber { get; set; }
     bool _isChecked;
 }
    
 public class MistHeaders
 {
     public List<Table> Table { get; set; }
 }
 }

Any help would be appreciated.

dotnet-xamarinformsdotnet-mobile-general
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers