We are busy developing a silverlight app that has to show some data in tabular form. The radgridview to be able to visualize random data. We want to use MVVM pattern.
Our solution is to format th input data into an observable collection of expando objects
we do that like this
private void CreateDataSourceList(ChartInfo results)
{
List<List<object>> theRows = results.TableData.Rows;
var titles = results.TableData.ColumnTitles.Keys.ToList();
TheListOfitems =
new ObservableCollection<dynamic>();
foreach (var rowItem in theRows)
{
dynamic expando = new ExpandoObject();
int counter = 0;
foreach (var columnName in titles)
{
// var columnNameFixed = System.Windows.Browser.HttpUtility.HtmlEncode(columnName);
var columnNameFixed = FixUpColumnName(columnName);
var p = expando as IDictionary<String, object>;
var columnString = rowItem[counter] as ColumnString;
if (columnString != null)
{
p[columnNameFixed] = columnString.ToString();
}
else
{
var columnDouble = rowItem[counter] as ColumnDouble;
if (columnDouble != null)
{
p[columnNameFixed] = columnDouble;
}
else
{
p[columnNameFixed] =
string.Empty;
}
}
counter++;
}
TheListOfitems.Add(expando);
}
}
WE hook up this observable collection (TheListOfitems) to radgridview in xaml like this.
<telerik:RadGridView x:Name="datagrid" Grid.Row="1" AutoGenerateColumns="True" IsReadOnly="True" ItemsSource="{Binding TheListOfitems}"
MaxColumnWidth="250" >
</telerik:RadGridView>
Note the autogeneratecolumns property. This works like a charm!
The problem is however the need for the function FixUpColumnName, we need this for the columnnames can contain strings like "abc(123>)", and these will
Not applying the function FixUpColumnName results in a bug (?): the radgridview will generate the column correctly, but the rows are empty. they are not filled with the corresponding data.
FixUpColumnName is a function that now will replace every strange character with a underscore like this
private
static string FixUpColumnName(string columnName)
{
Regex re = new Regex("[^ a-zA-Z0-9\n]");
return re.Replace(columnName,"_");
}
Our users however do want the original columnname to be displayed.
How can this be solved?
Is there some sort of escaping possible?
Kind Regards
Dennis Janssen
PRe Consultants
NEtherlands