c# - DataBinding not working properly -


i made code after deserialization populate datagrid there problem in population. code insert data in datagrid:

var leaguetable_object =          jsonconvert.deserializeobject<leaguetable.rootobject>(responsetext);  foreach (var classifica in leaguetable_object.standing) {     league_datagrid.items.add(new leaguetable.classifica(){         name = classifica.position + " " + classifica.teamname});     var name = new datagridtextcolumn();     name.binding = new binding("name");     league_datagrid.columns.add(name);      league_datagrid.items.add(new leaguetable.classifica(){                 points = classifica.points});     var points = new datagridtextcolumn();     points.binding = new binding("points");     league_datagrid.columns.add(points);      league_datagrid.items.add(new leaguetable.classifica(){                playedgames = classifica.playedgames});     var playedgames = new datagridtextcolumn();     playedgames.binding = new binding("playedgames");     league_datagrid.columns.add(playedgames);      league_datagrid.items.add(new leaguetable.classifica(){                goals = classifica.goals });     var goals = new datagridtextcolumn();     goals.binding = new binding("goals");     league_datagrid.columns.add(goals);      league_datagrid.items.add(new leaguetable.classifica(){                goalsagainst = classifica.goalsagainst });     var goalsagainst = new datagridtextcolumn();     goalsagainst.binding = new binding("goalsagainst");     league_datagrid.columns.add(goalsagainst);      league_datagrid.items.add(new leaguetable.classifica(){                  goalsdifference = classifica.goaldifference });     var goalsdifference = new datagridtextcolumn();     goalsdifference.binding = new binding("goalsdifference");     league_datagrid.columns.add(goalsdifference); } 

each datagrid in xaml have structure:

 <datagridtextcolumn binding="{binding path = 'name'}"            header="squadra"            canuserresize="true" minwidth="150" maxwidth="200"/> 

and class contain structure of data:

public struct classifica     {         public string name { get; set; }         public int points { get; set; }         public int position { get; set; }         public int playedgames { get; set; }         public int goals { get; set; }         public int goalsagainst { get; set; }         public int goalsdifference { get; set; }     } 

now data insert in datagrid, final result this: enter image description here

how can see data relating team placed on multiple lines , of course not good. furthermore, data included in columns not specified in binding-which quite strange. can explain me doing wrong?

update image propose solution:

enter image description here

xaml structure:

enter image description here

you looping through every record in data set , adding item grid once every column. need add columns once , item once.

var leaguetable_object =     jsonconvert.deserializeobject<leaguetable.rootobject>(responsetext);  datagridtextcolumn name = new datagridtextcolumn(); name.binding = new binding("name"); league_datagrid.columns.add(name);  datagridtextcolumn points = new datagridtextcolumn(); points.binding = new binding("points"); league_datagrid.columns.add(points);  datagridtextcolumn playedgames = new datagridtextcolumn(); playedgames.binding = new binding("playedgames"); league_datagrid.columns.add(playedgames);  datagridtextcolumn goals = new datagridtextcolumn(); goals.binding = new binding("goals"); league_datagrid.columns.add(goals);  datagridtextcolumn goalsagainst = new datagridtextcolumn(); goalsagainst.binding = new binding("goalsagainst"); league_datagrid.columns.add(goalsagainst);  datagridtextcolumn goalsdifference = new datagridtextcolumn(); goalsdifference.binding = new binding("goalsdifference"); league_datagrid.columns.add(goalsdifference);  foreach (var classifica in leaguetable_object.standing) {     league_datagrid.items.add(new leaguetable.classifica     {         name = classifica.position + " " + classifica.teamname,         points = classifica.points,         playedgames = classifica.playedgames,         goals = classifica.goals,         goalsagainst = classifica.goalsagainst,         goalsdifference = classifica.goaldifference     });  } 

also note not best way bind data. better off adding columns grid in xaml code example.

edit

after edit showing xaml code, it's clear don't need manually add columns instead need code inside foreach. also, can simplified further linq:

var leaguetable_object =      jsonconvert.deserializeobject<leaguetable.rootobject>(responsetext);  league_datagrid.items.addrange(     leaguetable_object.standing.select(c => new     {         name = c.position + " " + c.teamname,         points = c.points,         playedgames = c.playedgames,         goals = c.goals,         goalsagainst = c.goalsagainst,         goalsdifference = c.goaldifference     })); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -