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

How to manipulate bounded data before display?

3 Answers 189 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mehdi
Top achievements
Rank 1
Mehdi asked on 24 Jun 2012, 09:16 AM

Hi

I have a radgridview bounded to a list (i'm using entity framework for my db backend). i have some number and string fields and a list<string> field in my data list.

Now i want to convert that list<string> into comma separated string and then show it in it's grid column.

I know how to convert list into string(via string.join), but i don't know how could i tell to radgrid to show joint string.

So, which event of radgrid is a good place to change column or row data to display?

I mean an event that fires when radgrid is populating columns or rows data(with access to original bounded data).

Please help me.

3 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Petrov
Telerik team
answered on 27 Jun 2012, 07:47 AM
HelloMehdi,

Thank you for writing.

You can achieve your goal through RadGridView's conversion layer. You can read more on this in our online product documentation. Here is an example of how to implement it for your case:
public class ListToStringTypeConverter : TypeConverter
{
  public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  {
    return destinationType == typeof(List<string>);
  }
 
  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  {
    List<string> values = (List<string>)value;
 
    return string.Join(",", values);
  }
 
  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  {
    return sourceType == typeof(string);
  }
 
  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  {
    string values = (string)value;
 
    return new List<string>(values.Split(','));
  }
}

Then you have to assign an instance of this class to the DataTypeConverter property of the column.

I hope this will be useful for you. Should you have further questions, I would be glad to help. 
 
Greetings,
Ivan Petrov
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Mehdi
Top achievements
Rank 1
answered on 27 Jun 2012, 11:12 AM

Thanks Ivan. it really helps.

But for sake of curiosity, which event could be a good place to change cell value before it was shown in gridview:

CreateRow or CreateRowInfo or CellFormatting or something else ?

(With consideration of table element begin/end update or suspend/resume layout.)

0
Ivan Petrov
Telerik team
answered on 29 Jun 2012, 01:44 PM
Hi Mehdi,

Thank you for writing back.

Changing the value of the cell in any other place/event would require you to handle the conversion on different places, like editors, cell element texts etc. manually. If you would like to have a different visual representation of the cell value the CellFormatting is the best approach, but in your case you want to change the actual type of the value so the best approach is to use the conversion layer of the grid.  

I hope this will be useful. Feel free to write back, if any further questions arise.
 
Greetings,
Ivan Petrov
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Mehdi
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Mehdi
Top achievements
Rank 1
Share this question
or