c# - No updated data in detail DataGrid -


there 2 datagrid on form linked collectionviewsource. when update observablecollection parts updated datagridmaster. if click on it, data appears in datagriddetail1. question. how make data updated without mouse clicks?

xaml:

    <window.resources>          <collectionviewsource x:key="vsmaster"  source="{binding parts}"/>          <collectionviewsource x:key="vsdetail1" source="{binding parties_v1, source={staticresource vsmaster}}" />      </window.resources>        <datagrid x:name="datagridmaster" autogeneratecolumns="false" enablerowvirtualization="true"         itemssource="{binding source={staticresource vsmaster}}"  margin="0,10,346,384"         rowdetailsvisibilitymode="visiblewhenselected">      <datagrid x:name="datagriddetail1" isreadonly="true" selectionmode="single" autogeneratecolumns="false"         enablerowvirtualization="true"                 itemssource="{binding source={staticresource vsdetail1}}" issynchronizedwithcurrentitem="true"                 margin="0,149,346,215" rowdetailsvisibilitymode="visiblewhenselected">          public class part      public part(int id, string pnumber, string owner, int? priceid,          list<parties_v1> lparties)       {          id = id;          part_number = pnumber;          owner = owner;          price_id = priceid;          parties_v1 = lparties;      }      public int id { get; set; }        public string part_number { get; set; }        public string owner { get; set; }        public int? price_id { get; set; }        public list<parties_v1> parties_v1 { get; set; }

my viewmodel

class mainwindowviewmodel : baseviewmodel      {          public string npart {get; set;}           private observablecollection<partsview> _parts;          public observablecollection<partsview> parts          {                           {                  return _parts;              }              set               {                  _parts = value;                  onpropertychanged("parts");              }          }          public mainwindowviewmodel()          {              _parts = partsrepository.getstartdata();          }            relaycommand _findpartcommand;          public icommand findpart          {                           {                  if ( _findpartcommand == null)                       _findpartcommand = new relaycommand(executefindpartcommand, canexecutefindpartcommand);                  return _findpartcommand;              }          }            public void executefindpartcommand(object parameter)          {              _parts = partsrepository.getpart(npart);          }            public bool canexecutefindpartcommand(object parameter)          {              return true;          }

as didn't post complete code. so, created 1 myself see problem.

after doing changes, have refresh collection view using refresh().

    ((listcollectionview)datagridmaster.itemssource).refresh(); 

** old answer without vm support **

my sample app code :

<window x:class="wpfresources._32691328.win32691328"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="win32691328" height="661.015" width="506.391">     <window.resources>         <collectionviewsource x:key="vsmaster"  source="{binding parts}"/>         <collectionviewsource x:key="vsdetail1" source="{binding parties_v1, source={staticresource vsmaster}}" />     </window.resources>     <stackpanel>         <datagrid x:name="datagridmaster" autogeneratecolumns="true" enablerowvirtualization="true"        itemssource="{binding source={staticresource vsmaster}}"          rowdetailsvisibilitymode="visiblewhenselected"></datagrid>         <button content="updatepart" click="button_click"/>         <datagrid x:name="datagriddetail1" margin="0 10 0 0" isreadonly="true" selectionmode="single" autogeneratecolumns="true"              enablerowvirtualization="true"              itemssource="{binding source={staticresource vsdetail1}}" issynchronizedwithcurrentitem="true"              rowdetailsvisibilitymode="visiblewhenselected"/>     </stackpanel> </window>  using system; using system.collections.objectmodel; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.shapes;  namespace wpfresources._32691328 {     /// <summary>     /// interaction logic win32691328.xaml     /// </summary>     public partial class win32691328 : window     {         public win32691328()         {             initializecomponent();              this.datacontext = new datastore();         }          private void button_click(object sender, routedeventargs e)         {             list<parties_v1> p = new list<parties_v1>();             p.add(new parties_v1() { id = -1999 });             p.add(new parties_v1() { id = -1785 });             p.add(new parties_v1() { id = -1123 });              observablecollection<part> parts = (observablecollection<part>)((listcollectionview)datagridmaster.itemssource).sourcecollection;             parts[0].parties_v1 = p;              ((listcollectionview)datagridmaster.itemssource).refresh();         }     } }   using system; using system.collections.objectmodel; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks;  namespace wpfresources._32691328 {     public class datastore     {         observablecollection<part> _parts;         public observablecollection<part> parts { { return _parts; } set { _parts = value; } }          public datastore() {              list<parties_v1> p1 = new list<parties_v1>();             p1.add(new parties_v1() { id = 1999 });             p1.add(new parties_v1() { id = 1785 });             p1.add(new parties_v1() { id = 1123 });              list<parties_v1> p2 = new list<parties_v1>();             p2.add(new parties_v1() { id = 289 });             p2.add(new parties_v1() { id = 2644 });             p2.add(new parties_v1() { id = 2321 });              list<parties_v1> p3 = new list<parties_v1>();             p3.add(new parties_v1() { id = 3754 });             p3.add(new parties_v1() { id = 37543 });             p3.add(new parties_v1() { id = 31333 });              _parts = new observablecollection<part>();             _parts.add(new part(1, "123", "1abc", 111, p1));             _parts.add(new part(5, "3453", "2abc", 111, p2));             _parts.add(new part(7, "13433", "3abc", 111, p3));             _parts.add(new part(8, "2223", "4abc", 111, p1));             _parts.add(new part(10, "155553", "5abc", 111, p3));             _parts.add(new part(11, "122223", "6abc", 111, p2));          }     }      public class part     {         public part(int id, string pnumber, string owner, int? priceid,             list<parties_v1> lparties)         {             id = id;             part_number = pnumber;             owner = owner;             price_id = priceid;             parties_v1 = lparties;         }         public int id { get; set; }          public string part_number { get; set; }          public string owner { get; set; }          public int? price_id { get; set; }          public list<parties_v1> parties_v1 { get; set; }     }      public class parties_v1     {          public int id { get; set; }      } } 

** new answer vm support **

<window x:class="wpfresources._32691328.win32691328"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="win32691328" height="661.015" width="506.391">    <stackpanel>     <datagrid x:name="datagridmaster" autogeneratecolumns="true" enablerowvirtualization="true"                                 rowdetailsvisibilitymode="visiblewhenselected" />     <button content="updatepart" click="button_click"/>     <datagrid x:name="datagriddetail" margin="0 10 0 0" isreadonly="true" selectionmode="single" autogeneratecolumns="true"          enablerowvirtualization="true"           issynchronizedwithcurrentitem="true"          rowdetailsvisibilitymode="visiblewhenselected"/>    </stackpanel>     </window>  using system; using system.collections; using system.collections.objectmodel; using system.collections.generic; using system.windows; using system.windows.controls; using system.windows.data;  namespace wpfresources._32691328 {     /// <summary>     /// interaction logic win32691328.xaml     /// </summary>     public partial class win32691328 : window     {         datastore d;          public win32691328()         {             initializecomponent();              d = new datastore();             this.datacontext = d;              // bind itemssource of both grids              binding bi1 = new binding();             bi1.source = d.partsview;             bi1.mode = bindingmode.oneway;             bindingoperations.setbinding(datagridmaster, datagrid.itemssourceproperty, bi1);              binding bi2 = new binding();             bi2.source = d.partiesdetailview;             bi2.mode = bindingmode.oneway;             bindingoperations.setbinding(datagriddetail, datagrid.itemssourceproperty, bi2);         }          private void button_click(object sender, routedeventargs e)         {             d.change();                    }     } }  using system; using system.componentmodel; using system.collections.objectmodel; using system.collections.generic; using system.windows.data;  namespace wpfresources._32691328 {     public class datastore     {         observablecollection<part> _parts;         public observablecollection<part> parts { { return _parts; } set { _parts = value; } }          collectionviewsource _partsviewmaster;         public collectionviewsource partsview         {             { return _partsviewmaster; }         }          collectionviewsource _partsdetailview;         public collectionviewsource partiesdetailview         {             { return _partsdetailview; }         }          public datastore() {              _partsviewmaster = new collectionviewsource();             _partsdetailview = new collectionviewsource();                        list<parties_v1> p1 = new list<parties_v1>();             p1.add(new parties_v1() { id = 1999 });             p1.add(new parties_v1() { id = 1785 });             p1.add(new parties_v1() { id = 1123 });              list<parties_v1> p2 = new list<parties_v1>();             p2.add(new parties_v1() { id = 289 });             p2.add(new parties_v1() { id = 2644 });             p2.add(new parties_v1() { id = 2321 });              list<parties_v1> p3 = new list<parties_v1>();             p3.add(new parties_v1() { id = 3754 });             p3.add(new parties_v1() { id = 37543 });             p3.add(new parties_v1() { id = 31333 });              _parts = new observablecollection<part>();             _parts.add(new part(1, "123", "1abc", 111, p1));             _parts.add(new part(5, "3453", "2abc", 111, p2));             _parts.add(new part(7, "13433", "3abc", 111, p3));             _parts.add(new part(8, "2223", "4abc", 111, p1));             _parts.add(new part(10, "155553", "5abc", 111, p3));             _parts.add(new part(11, "122223", "6abc", 111, p2));               binding bc1 = new binding("parts");             bc1.source = this;             bindingoperations.setbinding(_partsviewmaster, collectionviewsource.sourceproperty, bc1);              binding bc2 = new binding("parties_v1");             bc2.source = _partsviewmaster;             bindingoperations.setbinding(_partsdetailview, collectionviewsource.sourceproperty, bc2);         }          public void change()         {             // changes              list<parties_v1> p = new list<parties_v1>();             p.add(new parties_v1() { id = -1999 });             p.add(new parties_v1() { id = -1785 });             p.add(new parties_v1() { id = -1123 });              _parts[0].parties_v1 = p;              // recreate view             _partsviewmaster.view.refresh();         }     }      public class part     {         public part(int id, string pnumber, string owner, int? priceid,             list<parties_v1> lparties)         {             id = id;             part_number = pnumber;             owner = owner;             price_id = priceid;             parties_v1 = lparties;         }         public int id { get; set; }          public string part_number { get; set; }          public string owner { get; set; }          public int? price_id { get; set; }          list<parties_v1> _parties = new list<parties_v1>();         public list<parties_v1> parties_v1 { { return _parties; } set { _parties = value; } }      }      public class parties_v1     {          public int id { get; set; }      } } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -