简体   繁体   中英

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 eg called Names that has get set methods inside it. Then use the following code to create 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.

修复了此问题,需要启用“自动生成列名”,只需要弄清楚如何重命名这些列标题即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM