How to set target null value for a column in SfDataGrid?

Hello, I'd like to ask if there is a way to set TargetNullValue for a column in SfDataGrid. Currently, if a property of an object model is null or empty then the whole column will be blank. Can I set the character "-" to replace the empty field? Thank you.


1 Reply 1 reply marked as answer

SV Suja Venkatesan Syncfusion Team July 10, 2022 06:29 PM UTC

Hi Josh,


We would like to let you know that you can achieve your requirement by setting DisplayBinding for the columns in GridLoaded event as like below code snippet.


Code Snippet:

XAML:

<sfgrid:SfDataGrid x:Name="sfGrid"

                   AllowSorting="True"

                   GridLoaded="sfGrid_GridLoaded"

                   ItemsSource="{Binding OrdersInfo}">


C#:

public partial class DataGridPage : ContentPage

{

  public DataGridPage()

  {

    InitializeComponent();

  }

  private void sfGrid_GridLoaded(object sender, GridLoadedEventArgs e)

  {

    var columns = (sender as SfDataGrid).Columns;

    foreach(var col in columns)

    {

      col.DisplayBinding= new Binding(col.MappingName, BindingMode.OneWay, new CustomConverter());

    }

  }

}

 

public class CustomConverter : IValueConverter

{

   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

   {

    if(value ==null)

    {

      return "-";

    }

    return value;

   }

 

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

   {

     return value;

   }

}


We have attached a sample based on your requirements. Please let us know if you need any further assistance.


Regards,

Suja.


Attachment: GettingStarted_8152483c.zip

Marked as answer
Loader.
Up arrow icon