c# - DataGrid replaces first row instead of adding a new row, -
i have datagrid in wpf populated observablecollection on doubleclick form datagrid (users click on preferred title , details datagrid populated associated data).
every time search new title, , make selection, details datagrid replaces first row of data there. how make datagrid adds new row can log multiple entities @ once exporting?
my code bellow:
datagrid .cs
// action seach imdb api associated infromation imdbid tagged title chose in listbox. private void movie_list_mousedoubleclick(object sender, routedeventargs e) { // grabs imdbid associated movie title selected used second api request. // datarowview row = (datarowview)movie_list.selecteditems; string titleid = ((searchinfo)movie_list.selecteditem).imdbid; string newurl = "http://www.omdbapi.com/?i=" + titleid + "&r=xml"; // prepares 2nd api url request data chosen title. // creates xml document store xml data sent api. var doc = xelement.load(newurl); // creates xml noedlist store values going associated given attribute tag. ienumerable<xelement> movielist = doc.descendants("movie"); // creats observablecollection object retrievalinfo displayed in movie_datagrid, can edited. observablecollection<retrievalinfo> gridinfo = new observablecollection<retrievalinfo>(movielist.select(movieelement => new retrievalinfo() { title = movieelement.attribute("title").value, actors = movieelement.attribute("actors").value.split(',').tolist(), genre = movieelement.attribute("genre").value, rated = movieelement.attribute("rated").value, imdbrating = movieelement.attribute("imdbrating").value, released = movieelement.attribute("released").value, runtime = movieelement.attribute("runtime").value, })); movie_datagrid.itemssource = gridinfo; }
retrievalinfo class (object used observablecollection)
public class retrievalinfo { public retrievalinfo() { actors = new list<string>(); } //creating list of info objects store returned data selected title. public string title { get; set; } public list<string> actors { get; set; } public string genre { get; set; } public string rated { get; set; } public string imdbrating { get; set; } public string released { get; set; } public string runtime { get; set; } } }
datagird xaml (movie_datagrid, holds details of movie selected)
<datagrid x:name="movie_datagrid" grid.columnspan="2" horizontalalignment="left" margin="292,107,0,0" verticalalignment="top" height="214" width="673" autogeneratecolumns="false" itemssource="{binding gridinfo}"> <datagrid.columns> <datagridtextcolumn header="title" binding="{binding path=title}"/> <datagridtextcolumn header="main actor 1" binding="{binding path=actors[0]}"/> <datagridtextcolumn header="main actor 2" binding="{binding path=actors[1]}"/> <datagridtextcolumn header="main actor 3" binding="{binding path=actors[2]}"/> <datagridcomboboxcolumn header="digital format" itemssource="{binding digital}"/> <datagridcomboboxcolumn header="physical format" itemssource="{binding physical}"/> <datagridtextcolumn header="genre" binding="{binding path=genre}"/> <datagridtextcolumn header="rated" binding="{binding path=rated}"/> <datagridtextcolumn header="imdb rating" binding="{binding path=imdbrating}"/> <datagridtextcolumn header="released" binding="{binding path=released}"/> <datagridtextcolumn header="runtime" binding="{binding path=runtime}"/> <datagridcomboboxcolumn header="file type" itemssource="{binding file_type}"/> </datagrid.columns> </datagrid>
before binding datagrid, should existing items list , add new item list , bind datagrid.
list<retrievalinfo> gridinfo = (list<retrievalinfo>)movie_datagrid.itemssource;
add new item list,
gridinfo.add(new retrievalinfo());
then bind list datagrid,
movie_datagrid.itemssource = gridinfo;
Comments
Post a Comment