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

filter on the combobox itemssource

Good morning,

I added a templatecolumn ina sfgrid. The celltemplate is a combobox with selectedvaluepath, selectedvalue.... and its itemssource is a different collectionviewsource than the one for the sfgrid. I would like to have in the columnheader filter not the mappingname values , but, for example, the displaymemberpath values of the combobox.

<Syncfusion:GridTemplateColumn MappingName="id_Comm"  AllowFiltering="True" FilterPopupStyle="{StaticResource popupstyleforOrderID}" >
                    <Syncfusion:GridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource CommesseVS}}" IsSynchronizedWithCurrentItem="False" 
                                                  SelectedValuePath="ID_Commessa" 
                                                  SelectedValue="{Binding Path=id_Comm}" 
                                                  DisplayMemberPath="Sigla_Commessa_Tecnel" />
                        </DataTemplate>
                    </Syncfusion:GridTemplateColumn.CellTemplate>
                </Syncfusion:GridTemplateColumn>

I can't figure how...
Hope to be clear, sorry for my english
Best regards and thank You

12 Replies

SR Sivakumar R Syncfusion Team January 25, 2016 04:48 AM UTC

Hi Marco,

You can achieve this requirement by setting GridTemplateColumn.DisplayBinding with convert the value to be displayed and GridTemplateColumn.UseBindingValue to true to load display binding values. Find the sample and code snippets below,

Sample:
http://www.syncfusion.com/downloads/support/forum/121745/ze/WpfTestingSample-1778893011

<Syncfusion:GridTemplateColumn MappingName="CountryCode" UseBindingValue="True">

    <Syncfusion:GridTemplateColumn.DisplayBinding>

        <Binding Path="CountryCode">

            <Binding.Converter>

                <local:DisplayMemberConverter DisplayMemberPath="CountryName"

                                                SelectedValuePath="CountryCode"

                                                DataContext="{StaticResource viewmodel}"/>

            </Binding.Converter>

        </Binding>

    </Syncfusion:GridTemplateColumn.DisplayBinding>

    <Syncfusion:GridTemplateColumn.CellTemplate>

        <DataTemplate>

            <ComboBox x:Name="combobox"

                        ItemsSource="{Binding Companies,Source={StaticResource viewmodel}}"

                        SelectedValue="{Binding Path=CountryCode}"

                        DisplayMemberPath="CountryName" 

                        SelectedValuePath="CountryCode" />

        </DataTemplate>

    </Syncfusion:GridTemplateColumn.CellTemplate>
</Syncfusion:GridTemplateColumn>


Converter class

public class DisplayMemberConverter : IValueConverter

    {

        public string DisplayMemberPath { get; set; }

        public string SelectedValuePath { get; set; }

        public object  DataContext { get; set; }


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

        {

            var selectedValue = value;

            IEnumerable list = null;

            var displayMemberPath = DisplayMemberPath;

            var valueMemberPath = SelectedValuePath;


            list = (DataContext as ViewModel).Companies;

           

            if (selectedValue == null)

                return null;


            PropertyDescriptorCollection pdc = null;


            if (string.IsNullOrEmpty(valueMemberPath))

            {

                if (!string.IsNullOrEmpty(displayMemberPath))

                {

                    var type = selectedValue.GetType();


                    pdc = TypeDescriptor.GetProperties(type);


                    return pdc.GetValue(selectedValue, displayMemberPath);

                }

                return selectedValue;

            }

            else

            {

                if (list == null)

                    return null;


                var enumerator = list.GetEnumerator();

                while (enumerator.MoveNext())

                {

                    var type = enumerator.Current.GetType();

                    pdc = pdc ?? TypeDescriptor.GetProperties(type);


                    if (selectedValue.Equals(pdc.GetValue(enumerator.Current, valueMemberPath)))

                    {

                        if (!string.IsNullOrEmpty(displayMemberPath))

                            return pdc.GetValue(enumerator.Current, displayMemberPath);

                        return enumerator.Current;

                    }

                }

            }

            return null;

        }


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

        {

            return value;

        }


    }



Thanks,
Sivakumar


MD marco del frate February 2, 2016 07:38 AM UTC

Hi Sivakumar,

it works ! thank you very much.
Now, if i can, i have other two questions: 

  1. in the same example, i'm using the  option ShowGroupDropArea="True". With the textcolumns is ok, while in the gridtemplatecolumn the grouping works but the value displayed are the "selectedvalue" while again i would like to show the displaymemberpath. 
  2. in the gridtemplatecolum the sorting works on the selectedvalue, while i would like it work on displaymeberpath. Is it possible?
Thank You very much for Your Support

Best regards

Marco


SP Sowndaiyan Paulpandi Syncfusion Team February 4, 2016 04:10 AM UTC

Hi Macro,

We have prepared a sample as per your requirement and you download the sample from the below location,
In SfDataGrid by default, the GroupCaption text is displayed in the {ColumnName}: {Key} – {ItemsCount} format. You can customize the GroupCaptionText by customizing the GridCaptionSummaryCellRenderer class and you can use the sortcomparer for achieve the customize sorting requirement,

Sample : http://www.syncfusion.com/downloads/support/forum/121745/ze/WpfTestingSample472262859

You can also refer the below KB for
customize the CaptionSummaryCell text

https://www.syncfusion.com/kb/3249/how-to-customize-the-captionsummarycell-text-in-the-sfdatagrid


Please the below UG for know more about sort comparer

http://help.syncfusion.com/wpf/sfdatagrid/sorting#custom-sorting



Regards,

Sowndaiyan





MD marco del frate February 10, 2016 11:26 AM UTC

Good morning Sowndaiyan

Thank You for Your support but i'm still facing some problem.
I modified the sample you send me (WpfTestingSample) adding the comparer. The sample works on grouping because the comparer is called when i drag the countrycode column header in the group area: on the group header is diplayed the countryname instead the countrycode.
The problem is on the sorting: when i press on the countrycode header to get sorting, the comparer is called, but the sorting seems to be still made on countrycode instead country name.

I wrote a sample (Elenchi2016) with dataset and datatables instead observable collections and in this case the comparer is called when i move the column header on the group area , but the group header shows the mappingname value instead the displaymemberpath. Morover, when click on the column header to get sort, the comparer is not called and the sort is on the mappingname value.

Please help me...
Hope to be clear

Thank You in advance

Best regards
Marco

Attachment: samples_1e03b735.zip


SR Sivakumar R Syncfusion Team February 11, 2016 01:38 PM UTC

Hi Marco,

We could not get your requirement clearly. Please find the modified sample where sorting, filtering and grouping works based on display value.

<Syncfusion:GridTemplateColumn MappingName="CountryCode" UseBindingValue="True">

    <Syncfusion:GridTemplateColumn.ValueBinding>

        <Binding Path="CountryCode">

            <Binding.Converter>

                <local:DisplayMemberConverter DisplayMemberPath="CountryName"

                                                SelectedValuePath="CountryCode"

                                                DataContext="{StaticResource viewmodel}"/>

            </Binding.Converter>

        </Binding>

    </Syncfusion:GridTemplateColumn.ValueBinding>

    <Syncfusion:GridTemplateColumn.CellTemplate>

        <DataTemplate>

            <ComboBox x:Name="combobox"

                        ItemsSource="{Binding Companies,Source={StaticResource viewmodel}}"

                        SelectedValue="{Binding Path=CountryCode}"

                        DisplayMemberPath="CountryName" 

                        SelectedValuePath="CountryCode" />

        </DataTemplate>

    </Syncfusion:GridTemplateColumn.CellTemplate>
</Syncfusion:GridTemplateColumn>


Sample:
http://www.syncfusion.com/downloads/support/directtrac/general/ze/WpfTestingSample-1640837721.zip

Refer below KB and modify renderer based on your requirement, If you want, group header to display based on MappingName.
https://www.syncfusion.com/kb/3249/

Thanks,
Sivakumar


MD marco del frate February 11, 2016 03:50 PM UTC

Hi Sivakumar,

and thank You for Your support.
Your sample works exactly as i want, while my sample still have some problem even if i changed as per Your suggestion, I can't hunderstand why....
Please find attached a word document with a better explanation (i hope) and my sample modified.
Summarising, the filter works using the displaymemberpath values (as i want), while sorting use the mappingname values (this is the problem)

Waiting for Your answer,

Thank You and best regards

Marco

Attachment: SampleExplanation_57ab41f3.zip


FP Farjana Parveen Ayubb Syncfusion Team February 12, 2016 01:58 PM UTC

Hi Marco,

We have analyzed your document, where you have using the same MappingName “Aziend”  for two columns.  In SfDataGrid, the Sorting, Grouping, Filtering operations are performed in each column based on MappingName. Hence the columns is not Sorted as expected, so we are recommended you to use the unique mapping name for each column to achieve your requirement.

Regards,
Farjana Parveen A


MD marco del frate February 13, 2016 08:52 AM UTC

Good morning,

i removed the first "Azienda" column as You sugegsted, but this do not fix the problem.
I made several tests and i noted that when i use observablecollections everithing works, while when i use datatables the sort does not work.
I think that the problem is that the Displaymemberconverter is not called when i press on the column header to get the sorting. So the sorting is made on the original mappingname and not based on the converter.
You can check it with the previous samples i sent You.
What Do You think about my hypothesis?
If i'm right,, is there a way to make it work with datatables?

Thank You again

Best regards

Marco


JS Jayapradha S Syncfusion Team February 15, 2016 04:52 PM UTC

Hi Marco,

Currently, we are checking the reported sorting problem with DataTable Collection. We will update the details by Feb 16th, 2016.

Thanks,
Jayapradha


JS Jayapradha S Syncfusion Team February 17, 2016 09:18 AM UTC

Hi Marco,

SfDataGrid does not support CustomSorting with DataTable collection. Since sorting and filtering will be done in DataTable itself. But you can converts the DataTable to Dynamic Object and make use of CustomSorting.

We have prepared a sample for your reference and find the sample from the following location,

Sample Link: http://www.syncfusion.com/downloads/support/forum/121745/ze/SfDataGridSample1714503220

Please find the stack overflow link to convert the DataTable to Dynamic object,

Reference Link: http://stackoverflow.com/questions/7794818/how-can-i-convert-a-datatable-into-a-dynamic-object 

Regards,
Jayapradha



MD marco del frate March 7, 2016 11:19 AM UTC

The solution You proposed it's quite diffucult to understand to me. First of all it's in C# and i'm facing some problem in translating in VB., but this is my problem.
Anyway, even if i study Your sample directly in C#, i can see that You convert a datatable in a dyanmic object, but i can't unserstand how this can help me when i need to manage the interaction between two tables. Sorry to bore You again but, if i'm not asking too much, i would like to see how to receive data from 2 datatables with a relation, for example "Contacts" and "Customer", where in the "Contacts" table there is a column with the id of the "Customer". My goal is to see in a sfdatagrid the contacts data (name, surname) and a columns that show the name of their company (customer), not just the id, and i would like to be able to filter, sort and grouping on this column with the customer name, not with the ID.

Contacts
idCon Name idCustomer
1 Marco Del Frate 1
2 Daniele Amato 1
3 Luciano Basteri 1
4 Giuseppe Benelli 2

Customer  
idCust Name
1 Microsoft
2 Apple

and on the sfdatagrid i would like to see

Name Customer
Marco Del Frate Microsoft
Daniele Amato Microsoft
Luciano Basteri Microsoft
Giuseppe Benelli Apple

and being able to sort,, filter and grouping on the column "Customer"

Thank You in advance

Best regards



SR Sivakumar R Syncfusion Team March 8, 2016 04:27 PM UTC

Hi Marco,

Please find the sample where we have converted DataTable to dynamic before binding to SfDataGrid to achieve your requirement.
this.EmployeeCollection = DataTableExtensions.ToDynamic(this.DTSource);

Sample:
http://www.syncfusion.com/downloads/support/forum/121745/ze/DataTableSample-2017615816

Thanks,
Sivakumar

Loader.
Up arrow icon