Background information:
I have the following code in a WCF service. GetDataTable returns a System.Data.DataTable based on a call to a SQL database with the query parameter passed.
public IEnumerable<Dictionary<string, object>> GetData(string *query*) {
var table = GetDataTable(query);
var columns = table.Columns.Cast<DataColumn>();
var dict = table.AsEnumerable()
.Select(r => columns.Select(c => new {
Column = c.ColumnName,
Value = r[c]
})
.ToDictionary(i => i.Column, i => i.Value != DBNull.Value ? i.Value : null));
return dict;
}
I have a Silverlight application that makes a call to GetData, passing a string, and I receieve results. However, the fields I have in my GridView are “Comparer”, “Count”, “Keys”, and “Value”.
Silverlight code snippet
WCFCLIENT oData = new WCFCLIENT ();
oData.GetData+= new EventHandler<GetDataCompletedEventArgs>(oData_GetData);
oData.GetData(*sqlquery*);
}
}
void oData_GetDataCompleted(object sender, GetDataCompletedEventArgs e) {
if (e.Error == null) {
rdpPaging.Source = e.Result;
rgvDataResults.ItemsSource = rdpPaging.Source;
}
My Question is two-fold
- Is the code I’m using to create the Dictionary wrong somehow?
- If the code is correct, how do I properly set the DataGrid’s data source so it shows the columns and rows returned by the SQL call?
I have tried binding to different properties of the e.Result variable, but have had similar results.
You have two options …
Bind the dictionary directly with the DataGrid but keep the columns non autogenerated. Create the columns manually by loopiing through all the keys in first item (dictionary) of the total list and use custom binding/converter for showing proper data.
I am using this with teleric GridView but I think this will work with normal Silverlight datagrid as well.
http://blogs.telerik.com/blogs/posts/09-04-23/lightweight-datatable-for-your-silverlight-applications.aspx
You can convert your list into datatable at clientside and use this approach easily.
[Update] Code example of the first option
Converter code. Please note you need to store the key in the converter that you pass in the constructor.