We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Template column - How to get one cell's value ?

Hi !
I use a SfDataGrid and i have 2 template column with pickers.
The problem is that i don't know how to get the selected value of these pickers in my viewModel 
Any one can help me ? Thanks ! :)

Here's my code : 

<?xml version="1.0" encoding="utf-8" ?>
<controls:KerhisContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                            x:Class="Kerhis.Domopig.View.Views.MouvementAnimauxPage"
                            x:Name="this"
                            xmlns:controls="clr-namespace:Kerhis.Core.Forms.Controls;assembly=Kerhis.Core.Forms"
                            xmlns:cr="clr-namespace:Kerhis.Domopig.View.Controls;assembly=Kerhis.Domopig.View"
                            xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
                            BindingContext="{Binding MouvementAnimaux, Source={StaticResource Locator}}"
                            Title="Mouvements Animaux">
  
  <controls:KerhisContentPage.Content>
    <ScrollView>
      <StackLayout VerticalOptions="FillAndExpand">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
      
        <Image Source="salle.png" Grid.Column="0" Grid.Row="0"/>
        <Label Text="Nom de la salle : " FontSize="Medium" HorizontalOptions="Start" VerticalOptions="Center" Grid.Column="1" Grid.Row="0"/>
        <Label Text="{Binding Salle.LibelleSalle}" FontSize="Medium" HorizontalOptions="Start" VerticalOptions="Center" Grid.Column="2" Grid.Row="0"/>
        <Label Text="Batiment : " FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center" Grid.Column="3" Grid.Row="0"/>
        <Label Text="{Binding Salle.Batiment.LibelleBatiment}" FontSize="Medium" HorizontalOptions="Start" VerticalOptions="Center" Grid.Column="4" Grid.Row="0"/>
        <Label Text="Site : " FontSize="Medium" HorizontalOptions="End" VerticalOptions="Center" Grid.Column="5" Grid.Row="0"/>
        <Label Text="{Binding Salle.Site.LibelleSite}" FontSize="Medium" HorizontalOptions="Start" VerticalOptions="Center" Grid.Column="6" Grid.Row="0"/>

        <Image Source="cochon.png" Grid.Column="0" Grid.Row="1"/>
        <Label Text="{Binding NombreAnimaux}" FontSize="Medium" HorizontalOptions="Start" VerticalOptions="Center" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"/>

      </Grid>
    
      <cr:KerhisDataGrid AutoGenerateColumns="False"
                         ItemsSource="{Binding ListeMouvementAnimaux}"
                         ColumnSizer="Star"
                         SelectionMode="SingleDeselect"
                         VerticalOptions="FillAndExpand">

        <cr:KerhisDataGrid.Columns x:TypeArguments="syncfusion:Columns">
          <syncfusion:GridTextColumn HeaderText="Date"
                                     MappingName="DateMouvement"
                                     TextWrapping="WordWrap"/>
        
          <syncfusion:GridTextColumn HeaderText="Effectif J-1"
                                     MappingName="AncienEffectif"
                                     TextWrapping="WordWrap"/>

          <syncfusion:GridTextColumn HeaderText="Effectif J-J"
                                     MappingName="EffectifActuel"
                                     TextWrapping="WordWrap"/>

          <syncfusion:GridTextColumn HeaderText="Différence"
                                     MappingName="Difference"
                                     TextWrapping="WordWrap"/>

          <syncfusion:GridTemplateColumn HeaderText="Qualification"
                                     MappingName="CodeQualificationMouvement"
                                     TextWrapping="WordWrap">
            
            <syncfusion:GridTemplateColumn.CellTemplate>
              <DataTemplate>
                <controls:KerhisPicker HorizontalOptions="FillAndExpand"
                                       ItemsSource="{Binding BindingContext.Qualifications, Source={x:Reference this}}"
                                       VerticalOptions="Center"/>
              </DataTemplate>
            </syncfusion:GridTemplateColumn.CellTemplate>
          </syncfusion:GridTemplateColumn>

          <syncfusion:GridTemplateColumn HeaderText="N° Bon Uniporc associé"
                                     MappingName="IDBordereau"
                                     TextWrapping="WordWrap">
            
            <syncfusion:GridTemplateColumn.CellTemplate>
              <DataTemplate>
                <controls:KerhisPicker HorizontalOptions="FillAndExpand"
                                       ItemsSource="{Binding BindingContext.ListeBordereaux, Source={x:Reference this}}"
                                       VerticalOptions="Center"
                                       IsEnabled="{Binding BindingContext.Enabled, Source={x:Reference this}}"/>
              </DataTemplate>
            </syncfusion:GridTemplateColumn.CellTemplate>
          </syncfusion:GridTemplateColumn>

          <syncfusion:GridTextColumn HeaderText="Commentaire"
                                     MappingName="Commentaire"
                                     TextWrapping="WordWrap"/>

        </cr:KerhisDataGrid.Columns>

      </cr:KerhisDataGrid>
    </StackLayout>
    </ScrollView>
  </controls:KerhisContentPage.Content>
</controls:KerhisContentPage>

3 Replies

JG Jai Ganesh S Syncfusion Team August 2, 2016 01:37 PM UTC

Hi Yasahbi, 
 
You can get the selectedvalue for the control that is defined inside the Templatecolumn by using the following code, 
 
<Syncfusion:GridTemplateColumn  MappingName="EmployeeArea" Width="200" Syncfusion:FocusManagerHelper.WantsKeyInput="True" AllowFiltering="True"> 
           <Syncfusion:GridTemplateColumn.CellTemplate> 
                 <DataTemplate> 
                        <ListBox Width="200" Height="24" Syncfusion:FocusManagerHelper.FocusedElement="True" ItemsSource="{Binding Path=DataContext.ListItems,ElementName=datagrid}" SelectedValue="{Binding Path=DataContext.SelectedValue,ElementName=datagrid}"/> 
                 </DataTemplate> 
        </Syncfusion:GridTemplateColumn.CellTemplate> 
                  
   </Syncfusion:GridTemplateColumn> 
 
In the above sample, we have load the ListBox inside the GridTemplateColumn, and get its SelectedValue from viewmodel by binding viewmdel property. 
 
Or else if you want to bind the underlying collection property inside the GridTemplateColumn by using the following code, 
 
<Syncfusion:GridTemplateColumn MappingName="EmployeeAge" AllowFiltering="True"> 
                     
      <Syncfusion:GridTemplateColumn.CellTemplate> 
                 <DataTemplate> 
                       <TextBlock Text="{Binding EmployeeAge}"/> 
                  </DataTemplate> 
      </Syncfusion:GridTemplateColumn.CellTemplate> 
                     
 </Syncfusion:GridTemplateColumn> 
 
 
Regards, 
Jai Ganesh S 



YA Yasahbi August 3, 2016 07:44 AM UTC

Hi Jai ! 
Thanks for answeringbut it doesn't resolve my problem.
The important part of my code is the following one : 

<syncfusion:GridTemplateColumn HeaderText="N° Bon Uniporc associé"
                                     MappingName="IDBordereau"
                                     TextWrapping="WordWrap">
            
            <syncfusion:GridTemplateColumn.CellTemplate>
              <DataTemplate>
                <controls:KerhisPicker HorizontalOptions="FillAndExpand"
                                       ItemsSource="{Binding BindingContext.ListeBordereaux, Source={x:Reference this}}"
                                       VerticalOptions="Center"
                                       IsEnabled="{Binding BindingContext.Enabled, Source={x:Reference this}}"/>
              </DataTemplate>
            </syncfusion:GridTemplateColumn.CellTemplate>
          </syncfusion:GridTemplateColumn>

As you can see, i use a Picker in a column, the problem is that i do'nt know how to get the selected value, because the Picker hasn't a SelectedValue Property.


JG Jai Ganesh S Syncfusion Team August 4, 2016 02:37 PM UTC

Hi Yasahbi,  
 
As we said in our previous update, you can bind the underlying class property or the viewmodel property inside the TemplateColumn. But in your code snippet, you have used the custom picker inside the TemplateColumn. In this case you have to write the SelctedValue property in custom picker and get the value from viewmodel through binding. 
 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon