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
close icon

DataGrid + GridTemplateColumn + IValueConverter and how about filtering?

Greetings!

I'm having a bit of trouble getting the DataGrid do as I wan't to.

Let's say, that I have a DataGrid, which I create a couple of columns. I map those too "Data[0].Value", "Data[1].Value" and so on. This works fine, except from filtering doesn't work with all cells, but it's not my main concern.

I have a column using a GridTemplateColumn. That template is defined as a DataTemplate:

<DataTemplate x:Key="myTemplate">
<Grid>
          <TextBlock Text="{Binding Converter={StaticResource CellConverter}}" />
</Grid>
</DataTemplate>

This works, sort of. If I place a breakpoint in my IValueConverter (CellConverter), it does execute, but the Value is not the value mapped to the cell. It's object mapped to the whole row, in this instance, an object containing an array with values.

Should the value in the converter be the mapped value? If no, how to I figure out what the mappingname of the cell is? My converter needs to know exactly what it has to convert - eg. the position in the array.

Hardcoding is no possibility, it's a part of a very dynamic system.

Now the second concern:

Filtering does not seem to work for cells mapped above. However if I make a mappingname as "Date" - where it doesn't enter a list or an array, it works fine. I DO set column.AllowFiltering to true. And the button it shown - it's just all blanks. If I try to sort the array cells, I get an exception. If I try to sort the "regular" cells, as "Date" it works like a charm.

//Nicky



21 Replies

RA Riyaj Ahamed I Syncfusion Team June 12, 2013 04:07 PM UTC

Hi Nicky,

 

Thanks for contacting Syncfusion support.

 

Please find the response for your queries below,

 

Query 1:

 

We have analyzed your query. We found that the issue has occured because of defining the binding without “Path” value. By setting the column mapping name as binding path you can get the corresponding cell value in the converter.

 

Query 2:

 

We have provided new features like DisplayBinding and ValueBinding in GridColumn to enhance data formatting in a column.

 

DisplayBinding : Sets the binding for GridColumns when the gridcell is in view mode.

ValueBinding : Sets the binding for GridColumns when the gridcell is in edit mode.

 

Intead of using GridTemplateColumns you can use the DisplayBinding and ValueBinding for data formatting. By default DataGrid performs sorting, filtering and grouping using reflection based on the column MappingName. Hence you must define the MappingName for the column. However DataGrid allows you to perform sorting,filtering and grouping based on ValueBinding by setting “UseBindingValues” property as true.

 

We have created the simple sample with Dynamic objects to demonstrate the DisplayBinding and ValueBinding. Please check the sample under the following location,

 

Samplehttp://www.syncfusion.com/downloads/Support/DirectTrac/109421/SampleTest-250128632.zip

 

Note: In the above sample DisplayBinding and ValueBinding applied for "Price" column.

 

Please let us know if you have any queries.

 

Regards,

Riyaj Ahamed I




NM Nicky Mogensen June 13, 2013 01:00 PM UTC

Hi.

Thanks for the answer.
The DisplayBinding did the trick about filtering and sorting.

But I still have a problem with the converter when using DataTemplates. If I do as you recommend - making a regular column and applies the binding to the DisplayBinding it works as it should. I need to be able to do this with a DataTemplate, and the name of the binding property is unknown until runtime.

And, how can I make a cell clickable, and meanwhile knowing exactly which cell in which row there's being clicked?

//Nicky


DV Dineshprabhu V Syncfusion Team June 17, 2013 10:28 AM UTC

Hi Nicky,

 

Thanks for your update and sorry for the inconvenience caused.

 

Query 1:

 

We have analysed the reported issue (problem with the converter when using DataTemplates) with our sample. If you are unable to set MappingName for the column then set unique key as MappingName for that column and pass that unique key as converter parameter. So that you can able to get the value based on that unique key.

 

To apply sorting, filtering and grouping for that column use the same binding value for DisplayBinding and ValueBinding. And also you have to set UseBindingValue as true.

 

We have created simple sample with GridTemplateColumn and please check the sample under the following location,

 

Sample: CustomSample.zip

 

Query 2:

 

We suspect that you might have used NavigationMode as Row in your application. Please set NavigationMode as Cell to make cell as clickable. By wire up CurrentCellActivated event of DataGrid you can get the information about which cell was clicked.

 

Please let us know if you have any concerns.

 

Thank you,

Dineshprabhu V

 



NM Nicky Mogensen June 18, 2013 03:04 PM UTC

I've looked at your sample, but I still can't get it working. I've attached a demo. When clicking the button the columns get's replaced by some templated. The app crashes, because it can't convert the ExpandoObject to an integer.

You were right about the second thing. It works pretty well, except that I get the row index according to the grid, and not the datasource - but this I might be able to make a workaround on my own, as I implement paging.



SyncfusionBugReplication_a15362a7.zip


NM Nicky Mogensen June 18, 2013 03:08 PM UTC

Seems that I can't attach the file. I e-mail it instead.


RA Riyaj Ahamed I Syncfusion Team June 21, 2013 11:48 AM UTC

Hi Nicky,

 

Thanks for your update.

 

We have analyzed your sample. We found that you are using Single DataTemplate for all the columns and the DataTemplate contains Binding without Path value. Hence you got the whole row DataContext as value in Converter. To overcome the issue you must define individual DataTemplate for every columns and set the path or converter parameter value in Binding.

 

Please let us know if you have any concern.

 

Regards,

Riyaj Ahamed I



RA Riyaj Ahamed I Syncfusion Team June 21, 2013 11:48 AM UTC

Hi Nicky,

 

Thanks for your update.

 

We have analyzed your sample. We found that you are using Single DataTemplate for all the columns and the DataTemplate contains Binding without Path value. Hence you got the whole row DataContext as value in Converter. To overcome the issue you must define individual DataTemplate for every columns and set the path or converter parameter value in Binding.

 

Please let us know if you have any concern.

 

Regards,

Riyaj Ahamed I



NM Nicky Mogensen June 24, 2013 10:14 AM UTC

But, if I do as you are saying, there's still no relation between the cells datacontext and what context my datatemplate receives. 

Does I understand it correct, when I say that when working with templated cells, the MappingName, DisplayBinding and so on, is completely useless? It's simply being ignored, and the datacontext for the cell is the same, as for the whole row?

So let's say in this scenario, when I have and always growing (atm. about 25) different needs of templating my columns, and there can be any number of column, controlled by the end-user, in any order at any index in a array, only known at runtime, this grid is completely useless? There is no way of making a datatemplate, and give a templated column a binding, without making dozens of the same datatemplate, with the only difference that each of them has a binding path  to a another index in the array?

Did this grid just put it self on any deathnote, when it comes to dynamic systems?


RA Riyaj Ahamed I Syncfusion Team June 26, 2013 07:14 AM UTC

Hi Nicky,

 

In DataGrid by default we are setting the whole row data as DataContext for the GridCells. To display particular column value along with some other column value in data template and to format the cell based on another column value for this scenarios we need the whole row data as DataContext.

 

You can override this behavior by writing the custom cell renderer to the data template cells. We have created the sample with custom Data template renderer to set the data context based on the mapping name. please find the sample in the following location

 

Sample: DataGridWithCustomRenderer.zip

 

And in the GridTemplateColumn we are using DisplayBinding and ValueBinding values for Sorting, Filtering and Grouping operations.

 

Regards,

Riyaj Ahamed I



NM Nicky Mogensen June 28, 2013 12:53 PM UTC

Hi.

Thank you so much for your time and help! This will help me alot :)

//Nicky


RA Riyaj Ahamed I Syncfusion Team July 5, 2013 04:01 AM UTC

Hi Nicky,

 

Thanks for your update.

 

Please let us know if you need further assistance.

 

Regards,

Riyaj Ahamed I



NM Nicky Mogensen July 29, 2013 11:41 AM UTC

Hi. After using the CustumGridCellTemplateRenderer-class posted above for some time, I now have a problem with filtering and paging.

When filtering, or changes page with the SfDataPager, the templated columns doesn't change value. The regular columns works as expected, but the values in the templated columns just never changes.

An example of a DataTemplate:

<DataTemplate x:Key="NumericTemplate">
<Grid>
<TextBlock Text="{Binding, Converter={StaticResource NumericConverter}}" />
</Grid>
</DataTemplate>

And the converter used :

<local:NumericConverter x:Key="NumericConverter" />

   public class NumericConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is Double)
                return ((Double)value).ToString("G29", CultureInfo.CurrentUICulture);

            if (value is Decimal)
                return ((Decimal)value).ToString("G29", CultureInfo.CurrentUICulture);

            return "";          
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

Thanks in advance, for helping me solve this.

//Nicky



NM Nicky Mogensen July 29, 2013 11:47 AM UTC

Please ignore that extra comma in the above code. (The binding)


NM Nicky Mogensen August 5, 2013 08:29 AM UTC

Anyone? :)


JG Jai Ganesh S Syncfusion Team August 5, 2013 10:48 AM UTC

Hi Nicky,

Thanks for your update.

We have analyzed your query. We would like to let you know that this issue was fixed in our latest version v11.2.0.29 which is available for download in following location,

Link:

  http://www.syncfusion.com/support/forums/winrt/109691/Syncfusion-Essential-Studio-for-  WinRT-%28XAML%29-v112029-%28Service-Pack%29-available-for-download-

 

We have updated the custom template cell renderer to overcome this issue and please check the sample under the following location,

Sample:

SyncfusionBugReplication.zip

Please let us know if you need further assistance.

Thank you,

Jai Ganesh S



NM Nicky Mogensen August 6, 2013 10:57 AM UTC

Thank you for the updated CustomGridCellTemplateRenderer... it did the trick. 

I did try to update this on my own, but that didn't go as well as this :)

Thanks!


NM Nicky Mogensen June 12, 2014 10:41 AM UTC

Hi again :)

Yesterday I updated my solution to the newest Syncfusion Essential Studio which broke the CustomGridCellTemplateRenderer.

I cannot download it again, it gives me an unathorized access exception, so I hope that this is still the original version.

I have made a sample, which replicates the most essential feature about my app, which uses OnDemandPaging. 

If you start typing "Nicky" in the textbox, one letter at the time, you'll see that the first column, the one which uses the template, is longer than the second column, that is a basic TextColumn. There is a total of 16 rows in my source, where the firstname is Nicky, and ALL of them has Mogensen as lastname. When the rowcount is decreasing to a smaller size than the pagesize (25), the templated cells keeps hanging.

The code distritibuted by Syncfusion is in the CustomCellGridTemplatedRenderer.cs-file, and the rest is hopefully OK. For some reason you have to type a letter and then delete it again, to see the complete source.

Second:
Sometimes, on a very rare occasion, it doesn't return the binding value, but the whole object. This is very random, but it's critical to the app.

Thanks, Nicky.

Attachment: DataGridTest2_8c29c970.zip


AR Akila Rajaram Syncfusion Team June 18, 2014 12:44 PM UTC

Hi Nicky,

 

We Couldn’t get your requirement clearly. Please share some more information which will be help us to provide better solution earlier.

 

Please let us know if you have any queries.

 

Regards,

Akila

 



NM Nicky Mogensen June 18, 2014 01:17 PM UTC

Hi

As you can see in my sample, my application uses a custom renderer for templated columns when using the SfDataGrid. The reason for this is a far as I understand, that my datasource is an object, which contains a collection of objects. My dataobjects are very dynamic, and I cannot change this model. Then I contacted you people in this thread, more precisely on June 26 2013, Riyaj Ahamed posted a sample containing this custom renderer. This have worked fine for some time, but now it struggles, and I don’t have access to re-download the provided sample posted in this thread anymore, so I honestly don’t know if I’m missing anything.

Anyways, the problem occurs when my search method returns a collection with less rows than the pagesize. As you can see in the sample, if you type “Nicky” in the textbox in the top of the screen, there are some rows where just the firstname is shown. Those rows does not exists. There is exactly 16 rows in the datasource with the firstname Nicky, and all of them has the lastname Mogensen.

The first column is a templated column, which uses the custom renderer provided almost a year ago. The second one is a basic TextColumn, and that one works just fine – it does not show anything on the empty rows just as it should.

This means, that there must have been an update to the SfDataGrid, which is causing the custom renderer to fail. It leaves the values hanging, instead of showing an empty cell when there is not enough rows.

That is why I’m trying to get some help, getting this to work again. It has been working, but it seems broken now. In the sample provided, I have taken the DataTable usercontrol from my solution and trimmed down all unnecessary code to replicate the problem, and modified for demo purposes of course.

In DataTable.xaml.cs on lines 167-212 is the method that performs the search and sets the data.

In DataTable.xaml.cs on lines 107-127 is the method that creates the grid columns, where you will see that I replace the renderer for templated columns with the custom one provided in this thread, and I create an StringColumn, which is a custom call that inherits from GridTemplatedColumn which is defined on lines 284-293.

The custom renderer is in the file CustomCellGridTemplateRenderer.cs, where I kind a seem to remember that I have corrected a bug. However, as I said earlier; I cannot download the provided sample, to see what I have changed and see if that is the cause of the error.

Hope this clarifies it a little bit.

 

Best Regards

Nicky

 



SR Sivakumar R Syncfusion Team June 21, 2014 03:48 AM UTC

Hi Nicky,

In previous version, if DataContext is null then InitializeDisplayElement method in renderer will not be called. But in current version, InitializeDisplayElement method will be called and as we are reusing the rows, we have to set the value based on DataContext. In the customer cell renderer, value is not reset if the DataContext is null. Now we have modified the sample to resolve this issue,

Please refer the following modified sample

Sample: DataGridTest2Modified.zip

Please let us know if you have any queries.

 

Regards,

Sivakumar



NM Nicky Mogensen June 21, 2014 03:46 PM UTC

Hi Sivakumar

Thanks man!

I'll try implementing this in the app as the first thing on monday.

Thanks for the help, this license is worth every penny in support!

Loader.
Live Chat Icon For mobile
Up arrow icon