This is a migrated thread and some comments may be shown as answers.

Add converter to autogenerated column

1 Answer 358 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 23 Jun 2017, 09:21 AM

Hi,

I have a RadGridView that I want to autogenerate columns for, but I also need to add converters to the bindings based on the datatype of the column. I got as far as creating a  to attach to the RadGridView.AutoGeneratingColumn event:

 

private void AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    if (e.ItemPropertyInfo.PropertyType == typeof(DateTimeOffset))
    {
        var col = e.Column;
        var binding = new Binding()
        {
            Converter = new Converters.DateTimeOffsetToLocalDateTimeConverter()
        };
         
        // How to add the converter to the columns binding?!?!
 
    }
}

 

I can't see any way to access the binding of the column to apply the converter. Am I missing something?

 

Thanks,

Pat

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan Ivanov
Telerik team
answered on 27 Jun 2017, 12:25 PM
Hi,

RadGridView operates with two types of columns: data-bound (GridVeiwBoundColumnBase) and non data-bound (GridViewColumn). When the auto-generating setting is used, a data-bound column is created for each property of the business object type. In this way, they can be accessed through the event arguments and their binding can be altered, like this:
private void gridAutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            var boundColumn = e.Column as GridViewBoundColumnBase;
            if (boundColumn != null && boundColumn.UniqueName == "Name")
            {
                boundColumn.DataMemberBinding.Converter = new MyConverter();
            }
        }


Regards,
Ivan Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Tim
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or