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

Using TypeConverters

2 Answers 246 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Christoffer
Top achievements
Rank 1
Christoffer asked on 27 Aug 2018, 08:31 AM

Hi i'm trying to understand how to use TypeConverters with databound columns.

The databound type is Int and I want to display a picture in a GridViewImageColumn based on that, but I can't make it work, the TypeConverter is never used by the RadGridView to convert the data?

Code:

TypeDescriptor.AddAttributes(typeof(int), new TypeConverterAttribute(
typeof(SeverityImageConverter)));
GridViewImageColumn Severity = new GridViewImageColumn("Severity", "Severity");
Severity.DataTypeConverter = TypeDescriptor.GetConverter(typeof(int));
IssueOverviewGrid.MasterTemplate.Columns.Add(Severity);
IssueOverviewGrid.DataSource = MasterBindingSource;

Type converter implementation:

public class SeverityImageConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
   Type sourceType)
{
if (sourceType == typeof(int))
return true;
else
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
   System.Globalization.CultureInfo culture, object value)
{
// if no value passed along
if (value == null)
throw new ArgumentNullException("value");

// if the source is a string then convert to our type
if (value is int)
{
// get strongly typed value
int? SeverityValue = value as int?;
Image SeverityImage = Library.GetIssueSeverityIcon(SeverityValue.Value);

return SeverityImage;
}

// otherwise call the base converter
else
return base.ConvertFrom(context, culture, value);
}
}


2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Aug 2018, 08:30 AM
Hello, Christoffer,           
 
After reviewing the custom TypeConverter's implementation, I have noticed that you are missing overriding the ConvertTo and CanConvertTo methods. I have prepared a sample project for your reference demonstrating how to convert the integer value to an Image. Additional information about converting data types is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/columns/converting-data-types

public class SeverityImageConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(Image))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }
 
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value == null)
            throw new ArgumentNullException("value");
 
        if (value is int)
        {
            int? SeverityValue = value as int?;
            Image SeverityImage = GetIssueSeverityIcon(SeverityValue.Value);
 
            return SeverityImage;
        }
        else
            return base.ConvertTo(context, culture, value, destinationType);
    }
 
    private Image GetIssueSeverityIcon(int p)
    {
        if (p == 0)
        {
            return Properties.Resources.Checked;
        }
        else
        {
            return Properties.Resources.Exclamation;
        }
    }
}



I hope this information helps. If you have any additional questions, please let me know.  
 
Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Kapil
Top achievements
Rank 1
Iron
commented on 29 May 2021, 12:45 PM

Hii i wanna know is there any way to call convertTo method asynchronously i m having same thing the only differece is i m getting image from url so i don't want to wait until it fatch image from url .
Dess | Tech Support Engineer, Principal
Telerik team
commented on 31 May 2021, 11:06 AM

Hello, Kapil,

In the ConvertTo method of the TypeConverter, you are expected to return an Image value that is expected by the GridViewImageColumn. Downloading the image from an external resource is more like a general programming question. That is why I have researched in the appropriate forums on this topic.

The following thread seems to be applicable for your case demonstrating how to use a HttpWebRequest which response is expected to return an image:
https://stackoverflow.com/questions/31272838/who-can-download-this-image-with-c-sharp-webrequest

Thus, the desired image is displayed on my end:

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

0
Christoffer
Top achievements
Rank 1
answered on 29 Aug 2018, 12:17 PM
Thank you very much. Excellent Support!
Tags
GridView
Asked by
Christoffer
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Christoffer
Top achievements
Rank 1
Share this question
or