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

GridTemplateColumn with button throw System.NullReferenceException

Hello everyone,

I'm trying to implement a SfDataGrid with a button that allows to interact with the element, but if the NavigationMode is set to Cell, everytime the button is clicked a NullReferenceException is thrown. This dosen't happen with NavigationMode=Row, but I need to be able to edit the entries, so in order to allow editing I need it to be NavigationMode=Cell.

To show the problem, I slightly changed the example "Data binding" in control Data Grid in Essential Studio 15.2.0.46.
     
Changes:
  •      In file ListViewPage.xaml:

<sync:SfDataGrid x:Name="syncgrid"

AllowFiltering="True"

AutoGenerateColumns="False"

ColumnSizer="Star"

ItemsSource="{Binding OrdersListDetails}"

NavigationMode="Cell">

<sync:SfDataGrid.Columns>

<syncfusion:GridTextColumn HeaderText="Order ID" MappingName="OrderID" TextAlignment="Left" />

<syncfusion:GridTextColumn HeaderText="Customer ID" MappingName="CustomerID" />

<syncfusion:GridTextColumn HeaderText="Employee ID" MappingName="EmployeeID" TextAlignment="Right" />

<syncfusion:GridTextColumn HeaderText="Ship City" MappingName="ShipCity" />

<syncfusion:GridTextColumn HeaderText="Ship Country" MappingName="ShipCountry" />

<syncfusion:GridTextColumn DisplayBinding="{Binding Path=Freight, StringFormat='{}{0:c}'}" TextAlignment="Right" />

<syncfusion:GridTemplateColumn Width="50" MappingName="" AllowFiltering="False" AllowSorting="False">

<syncfusion:GridTemplateColumn.CellTemplate>

<DataTemplate>

<Button Width="30" Height="30" Command="{Binding ElementName=syncgrid, Path=DataContext.EditCommand}" CommandParameter="{Binding}">

Edit

</Button>

</DataTemplate>

</syncfusion:GridTemplateColumn.CellTemplate>

</syncfusion:GridTemplateColumn>

</sync:SfDataGrid.Columns>

</sync:SfDataGrid>

Added TemplateColumn with button and changed NavigationMode to Cell.


  • In file OrderInfoViewModel.ch:

namespace DataBindingDemo

{

public class Command<T> : ICommand

{

public event EventHandler CanExecuteChanged;

public Action<T> Action;


public bool CanExecute(object parameter)

{

return true

}

public void Execute(object parameter)

{

Action.Invoke((T)parameter);

}

}


public class OrderInfoViewModel : NotificationObject

{

public ICommand EditCommand

{

get

{

return new Command<Orders>()

{

Action = (order) => { /*DO things with order*/ }

};

}

}


public OrderInfoViewModel()

{

OrdersListDetails = new OrderInfoRepository().PopulateOrders(200);

}


...


Added command class and EditCommand to OrderInfoViewModel class.


Everything else is unchanged.Hope it's clear enouph. Whenever the button "Edit" is pressend the Exception is thrown. Can someone explain what I'm doing wrong? As I said changing the NavigationMode to Row make the code run without problems. Am I missing something?


Cheers Antoine


1 Reply

GT Gnanasownthari Thirugnanam Syncfusion Team July 31, 2017 06:04 PM UTC

Hi Antoine, 
 
We have analyzed your query based on your given code snippet. We have found that in your code you have missed the MappingName for GridTemplateColumn. We have prepared the sample based on your given code snippet, you can download the same from below mentioned location.  
If it is different from your requirement please revert by modified sample. It will be helpful for us to analyze further on this. 
 
Sample location: 
 
You can use GridUnBoundColumn with some unique name for MappingName if you don't want to map any data field.  
Refer the below UG link: 
 
Regards, 
Gnanasownthari T. 
 


Loader.
Up arrow icon