Binding controls in a GridTemplateColumn to a DataTable

Hi,

I am having some trouble figuring out how to bind my XAML controls in a GridTemplateColumn to the DataTable values when binding the sfDataGrid to a DataTable.  I have a simple ContentPage that binds to a simple ViewModel.  Here is the ViewModel:

using System.Data;
namespace ItemsList.ViewModels
{
class ItemsVM : VMBase
{
private DataTable _testTable = new DataTable();
public DataTable TestTable { get => _testTable; set => _testTable = value; }

public ItemsVM()
{
_testTable.Columns.Add("ItemNumber", typeof(int));
_testTable.Columns.Add("ItemName", typeof(string));

_testTable.Rows.Add(1, "Item 1");
_testTable.Rows.Add(2, "Item 2");
}
}
}

And here is the ContentPage:

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:vm="using:ItemsList.ViewModels"
         xmlns:grid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
         mc:Ignorable="d"
             x:Class="ItemsList.Views.ItemsListPage">
    <ContentPage.Content>
<StackLayout Orientation="Vertical">
<StackLayout.BindingContext>
<vm:ItemsVM />
</StackLayout.BindingContext>
<grid:SfDataGrid x:Name="sfGrid"
ColumnSizer="Auto"
AllowEditing="False"
NavigationMode="Cell"
ItemsSource="{Binding TestTable}"
AutoGenerateColumns="True">
</grid:SfDataGrid>
</StackLayout>
</ContentPage.Content>
</ContentPage>

This works ok when the sfGrid auto-generates the columns.  I'm seeing all the values.  However, I want to use a GridTemplateColumn to display the ItemName in a Label, so I can use my own formatting.  But I cannot figure out how to bind to the values of the ItemName column.  I'm doing the following (the label binding is highlighted):

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:vm="using:ItemsList.ViewModels"
         xmlns:grid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
         mc:Ignorable="d"
             x:Class="ItemsList.Views.ItemsListPage">
    <ContentPage.Content>
<StackLayout Orientation="Vertical">
<StackLayout.BindingContext>
<vm:ItemsVM />
</StackLayout.BindingContext>
<grid:SfDataGrid x:Name="sfGrid"
 ColumnSizer="Auto"
 AllowEditing="False"
 NavigationMode="Cell"
 ItemsSource="{Binding TestTable}"
 AutoGenerateColumns="True">
<grid:SfDataGrid.Columns>
<grid:GridTemplateColumn HeaderText="Name"
 MappingName="ItemName">
<grid:GridTemplateColumn.CellTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Path=Item[1]}" />
</StackLayout>
</DataTemplate>
</grid:GridTemplateColumn.CellTemplate>
</grid:GridTemplateColumn>
</grid:SfDataGrid.Columns>
</grid:SfDataGrid>
</StackLayout>
</ContentPage.Content>
</ContentPage>

However, this is not working.  I'm getting the following exception:

System.Reflection.AmbiguousMatchException
  Message=Ambiguous match found.

How do I bind the properties of the Label in a GridTemplateColumn to the values in the DataTable?

Thanks a bunch for all your hard work and your assistance!

Alfredo


2 Replies 1 reply marked as answer

AL Alfredo June 1, 2020 07:34 PM UTC

I apologize, this issue has been resolved.  Shortly after I wrote the problem above I found out how to do this.  The correct binding syntax for the label is the following:

<Label Text="{Binding [1]}" />

Sorry for the trouble!

Alfredo



KK Karthikraja Kalaimani Syncfusion Team June 2, 2020 08:35 AM UTC

 
Hi Alferdo,

Thank you for the update. We glad to know that your requirement has been achieved. Please let us know if you have any further queries on this. We are happy to help you.

Regards,
Karthik Raja 


Marked as answer
Loader.
Up arrow icon