Migrating a Winforms DataTable app to WPF Datagrids {Any Advice?}

Greetings CSharp,

I've been attempting to port over a ASP.net Webforms app to a WPF client, here's some of the things I've had to change.

1) Replace WebClient with HttpClient Async. (for grabbing the JSON from a remote api)

2) Replace Datatables with autobind for Datagrid.

3) Contiune to use the JSON.net Lib

Here's an current example of the WPF project: http://ift.tt/1HahPRS

The second part has caused me some major problems in hindsight, The first being the fact I only need certain types of JSON data such as the Name, StatusDetails and Reason as autobind binds all the data inside a JSON string. In DataTables I was able to control what types of data could be added to each grid by doing a simple foreach expression.

 DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Name", typeof(string))); dt.Columns.Add(new DataColumn("Status", typeof(string))); dt.Columns.Add(new DataColumn("Reason", typeof(string))); foreach (var line in data) { var firstLineStatus = line.lineStatuses.FirstOrDefault(); var lineDescription = ""; var lineStatusDescription = "No Status Provided."; if (firstLineStatus != null) { lineStatusDescription = firstLineStatus.statusSeverityDescription; lineDescription = firstLineStatus.reason; } DataRow dr = dt.NewRow(); dt.Rows.Add(line.name, lineStatusDescription, lineDescription); } GridView1.DataSource = dt; GridView1.DataBind(); } 

In addition I'm also trying to figure out a way to get the grid to react to certain permeters such as showing the color red on the status table for any services that are closed or expecting delays.

 protected void RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[1].Text == "Good Service") { e.Row.Cells[1].BackColor = Color.LightGreen; } else if (e.Row.Cells[1].Text == "Severe Delays") { e.Row.Cells[1].BackColor = Color.LightPink; } else if (e.Row.Cells[1].Text == "Minor Delays") { e.Row.Cells[1].BackColor = Color.Yellow; } else if (e.Row.Cells[1].Text == "Special Service") { e.Row.Cells[1].BackColor = Color.PowderBlue; } else e.Row.Cells[1].BackColor = Color.White; } } 

The above examples are taken from my original WebForms app.

I'm currently trying to figure out a way of logically doing a simular expression with Datagrids.

I'm also wondering if this is possible and if so how in the more modern WPF enviroment? I would be truely greatful for any tips/examples that could help me along the way to getting this figured out, expalantions included.

Problems IEnumerable Data doesn't seem to want to bind directly to a Datagrid. For example lineStatuses should return the current status value from the API for some reason in WPF this isn't happening at all. The grid binds but the data tables are blank.

No possible way of binding data manually for each Json Parameter without having to edit a list object?

Many Thanks Oliver

by Oli_Picard via /r/csharp

Leave a Reply