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.
Unfortunately, activation email could not send to your email. Please try again.

DataGrid Binding

Thread ID:

Created:

Updated:

Platform:

Replies:

126037 Sep 20,2016 05:25 AM Sep 21,2016 08:49 AM Xamarin.Forms 1
loading
Tags: SfDataGrid
Thomas Gruener
Asked On September 20, 2016 05:25 AM

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)
        {
            try
            {
                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());
                dGrid.Columns.Add(col);
            }           

            return dGrid;
        }

Appreciate any help.
Thanks

Divakar Subramaniam [Syncfusion]
Replied On September 21, 2016 08:49 AM

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.  
 
Regards, 
Divakar. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;