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

Dynamic GridViewColumns filter function throw exception

1 Answer 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 26 Sep 2013, 07:49 PM
I have a GridView where the GridViewColumns are created dynamically. We now have data in a column that can sometimes be null. I have kind of figured out how to use the Converter from the binding to change the value based on the value coming in - but this did not fix it. When I click on the filter It throws the error that must be of type Int64.

Here is the code that builds the column:
GridViewDataColumn col = new GridViewDataColumn();               
                col.Header = column.Name;               
                col.DataMemberBinding = binding;               
                col.DataType = column.DataType;                               
                col.IsSortable = true;
 
                if (column.DataType == typeof(Int64))
                {
                    col.DataMemberBinding.Converter = new LongConverter();
                }
 Here is the converter: 
public class LongConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //Int64? convertedValue = (Int64)value;
            //if (convertedValue.HasValue)
            //{
            //    return convertedValue.Value;
            //}
            //else
            //{
            //    return "";
            //}
 
            return "N/A";
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.ToString().ToUpper() == string.Empty)
            {
                return new Int64();
            }
            else
            {
                return Int64.Parse(value.ToString());
            }
        }
    }
You can see I commented all most of the code out on the converter. I was just trying to get it to work. The value was coming back as the type - so I'm not sure whats going on there.

So I really have to issues here:
1) How to properly set the converter on the column datamember binding.
2) Why does the filter still recognize the null value even though I set the value to "N/A"

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 01 Oct 2013, 11:43 AM
Hello,

You get an exception as you try to cast the "N/A" string to an Int64 value. Please make sure that you convert only valid values.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or