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

DataGrid Binding

How can I do the Binding of my Converter with the DataGrid in Code, and how can I bind on ColumnGeneration?

this doesn't work for me:
col.DisplayBinding = new Binding("Vals[" + key.Key + "]", BindingMode.OneWay, new GridCurrencyConverter());

Here is my code:

public class GridCurrencyConverter : IValueConverter
        //check data and replace "-1" with "-"
        public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)
                string tmp = Helper.DataGridValueConverter(value.ToString());

                if (tmp != "-" && (tmp.Contains(".") || tmp.Contains(",")))
                    double val = System.Convert.ToDouble(value);
                    val = Math.Round(val);
                    return Helper.ConvertValue(val.ToString());

                return tmp;
            catch (Exception)
                return value;

        //keep current data
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo)
            return value;

 private SfDataGrid CreateDataGrid(List<DynamicData<string, object>> dynData)
            List<Dictionary<string, string>> objList = CreateGridItems(dynData);

            ViewModel vm = new ViewModel();
            vm.dataGridDetails = new ObservableCollection<DataGridItem>();

            foreach(Dictionary<string, string> di in objList)
                vm.dataGridDetails.Add(new DataGridItem() { Vals = di });

            dGrid = new SfDataGrid();
            dGrid.ItemsSource = vm.DataGridDetails;
            dGrid.ColumnSizer = ColumnSizer.None;
            dGrid.AlternationCount = 2;
            dGrid.GridStyle.AlternatingRowColor = Color.FromHex("#d3d3d3");

            dGrid.AutoGenerateColumns = false;
            dGrid.AllowSorting = true;

            foreach (KeyValuePair<string, string> key in objList[0])
                GridTextColumn col = new GridTextColumn();
                col.MappingName = "Vals[" + key.Key + "]";
                col.HeaderText = SprocEvaluator.TagConverter(key.Key);
                col.Width = 120d;
//this doesn't work: Cannot convert from GridCurrencyConverter to Xamarin.Forms.IValueConverter????
                col.DisplayBinding = new Binding("Vals[" + key.Key + "]", BindingMode.OneWay, new GridCurrencyConverter());

            return dGrid;

Appreciate any help.

1 Reply

DS Divakar Subramaniam Syncfusion Team September 21, 2016 12:49 PM UTC

Hi Thomas,   
Thanks for contacting Syncfusion Support.    
We have checked your query. We were not able to reproduce the reported issue and the converter is working fine from our side.   
We have prepared a sample by converting the negative value present in the collection to its equivalent positive value using IValueConverter inGridColumn.DisplayBinding property as per your requirement and you can download the same from the below link.   
In case, if the issue still persists at your end then please do revert with the modified sample reproducing the issue.  

Live Chat Icon For mobile
Up arrow icon