DataGrid and Silverlight
I am trying to develop a Silverlight app which needs to populate a datagrid from PHP. I have the PHP working fine using the JSON format and Silverlight can read in the data but how can I add this data to a datagrid. I was looking at adding a new programmatically like you can in C# WF using DataRow but this doesn't seem to be available in Silverlight.
I have recently found out how I can do this by creating a class e.g. called Names that has get set methods inside it. Then use the following code to c开发者_如何转开发reate the datasource for the datagrid
List<Names> source = new List<Names>();
foreach (JsonValue item in arrayJson)
{
string firstName = item["FirstName"].ToString().Replace('"', ' ').Trim();
string lastName = item["LastName"].ToString().Replace('"', ' ').Trim();
string age = item["Age"].ToString().Replace('"', ' ').Trim();
source.Add(new Names()
{
FirstName = firstName,
LastName = lastName,
Age = age
});
//MessageBox.Show("First Name: " + firstName + "\nLast Name: " + lastName + "\nAge: " + age, "Names", MessageBoxButton.OK);
}
tblGrid.ItemsSource = source;
However, when this code is used it creates a blank row for the number of records in the database no text. When I debug it and I look inside the source collection of items it shows all the correct values but the datagrid shows up blank rows.
I typically create a POCO (Plain Old CLR Object - or a Value Object) and then use the JSON libs in the Silverlight .Net DLLs to parse the JSON return into a collection of my POCOs. Something like this...
using System.Json;
public void DoArticleSearch()
{
WebClient proxy = new WebClient();
proxy.OpenReadAsync(new Uri(uriString));
proxy.OpenReadCompleted += (s, e) =>
{
if (e.Error != null)
{
string errorMsg = e.Error.Message;
}
JsonObject completeResult = (JsonObject)JsonObject.Load(e.Result);
string jsonOffset = completeResult["offset"].ToString();
string jsonTotal = completeResult["total"].ToString();
JsonArray resultsArray = (JsonArray)completeResult["results"];
ObservableCollection<Article> localArticles;
if (Offset == 0)
{
localArticles = new ObservableCollection<Article>();
}
else
{
localArticles = Articles;
}
foreach (JsonObject obj in resultsArray)
{
Article a = new Article();
if (obj.Keys.Contains("body"))
{
a.Body = obj["body"];
}
if (obj.Keys.Contains("byline"))
{
a.ByLine = obj["byline"];
}
if (obj.Keys.Contains("date"))
{
a.Date = a.FormattedDateTime(obj["date"]);
}
if (obj.Keys.Contains("title"))
{
a.Title = obj["title"];
}
if (obj.Keys.Contains("url"))
{
a.Url = new Uri(obj["url"]);
}
localArticles.Add(a);
}
Articles = localArticles;
};
}
The above sample does not include all of the using statements or property definitions that are used within the method call. The method resides in my view model and the Articles property is bound to a visual element (a datagrid) on the actual Silverlight page.
If you are developing in Silverlight for Windows Phone, however, you will need to use the third party JSON.Net library.
Fixed it, needed to have Auto Generate Column names enabled just need to work out how to rename these column headers.
精彩评论