Set max length of strings in radgrid / limit string length in radgrid with autogenerate columns
Question
Is there any way to tell a radgrid to truncate the length of strings in its columns? Im using a radgrid to show data from a sql view which has several (many, many) fields, so I want to use the autogeneratefields="true" rather than setting up all of the gridboundcolumns, and/or doing the truncation in the itemdatabound event or anything like that.
I suppose I could ask it this way also, what would be a good approach to getting data back from a view and have the fields only return the first x number of characters? As I said there are many fields so I don't want to have to code out every column in my dal and do a substring or something like that....
Any thoughts?
Thanks!
Rusty
Solution
Not sure if my question made sense, but in case anyone comes across a similar scenario, I ended up looping through my object's properties and truncated the strings before setting the datasource on the radgrid.
public static void SM_Dump_TruncStrings(ref List<myDataType> dump, int maxLength, bool addEllipses)
{
foreach (var sm in dump)
{
PropertyInfo[] infos = sm.GetType().GetProperties();
foreach (var info in infos)
{
if (info.PropertyType == typeof(string))
{
var origValue = info.GetValue(sm, null) as string;
if (origValue != null && origValue.Length > maxLength)
{
var newVal = origValue.Substring(0, maxLength);
if (addEllipses)
newVal += "...";
info.SetValue(sm, newVal, null);
}
}
}
}
}
This approach was taken from here : How to iterate through each property of a custom vb.net object?
cheers,
rusty