c# - Wpf - Binding one gridcontrol selected row data and populate it in another gridcontrol -


i have 1 gridcontrol various fields havent mentioned in code

<dxg:gridcontrol   horizontalalignment="stretch" height="300"  verticalalignment="top" x:name="grid1"  autopopulatecolumns="false" itemssource="{binding collection1}"    >                     <dxg:gridcontrol.view >                         <dxg:tableview x:name="tableview1" />                     </dxg:gridcontrol.view>                 .                    .                    .                    . 

i have grid control on same page various fields

<dxg:gridcontrol horizontalalignment="stretch" height="250"  verticalalignment="top" x:name="grid2"  autopopulatecolumns="false"                                  itemssource="{binding elementname="tableview1" ,path=collection2.focusedrow}"    >                     <dxg:gridcontrol.view >                         <dxg:tableview x:name="tableview2"  />                     </dxg:gridcontrol.view>                 .                    .                    .                    . 

now collection1 id primary key , collection2 colid foreign key both having relationship each other

scenario here if select row in grid1 corresponding records must displayed in grid 2

 public class mycollection: bindinglist<orders> {      public datacontext dc;      public mycollection(ilist<orders> list)         : base(list)     {     }      protected override void removeitem(int index)     {         orders deleteitem = this.items[index];          if (dc.order != null)         {             dc.order.deleteonsubmit(deleteitem);         }         base.removeitem(index);     }  } 

my generic class orders , generic class master same

if speak in terms of xaml properties, here want update itemssource property of 2nd datagrid on basis of selecteditem property of 1st datagrid.

to achieve this, add new property "selecteditemdg1" in viewmodel hold selection of 1st datagrid. in setter of "selecteditemdg1" property, set collection2 per need.

make sure implement inotifypropertychanged interface , use observablecollection type both collections.

following code sample same :

model classes:

public class country {     public string countryname { get; set; }      public int countryid { get; set; }      public list<state> states { get; set; } }  public class state {     public string statename { get; set; }     public int stateid { get; set; } } 

viewmodel :

public class mainwindowviewmodel : inotifypropertychanged {      public mainwindowviewmodel()     {         countriescollection = new observablecollection<country>();         statecollection = new observablecollection<state>();         loaddata();     }      private observablecollection<country> _countriescollection;      public observablecollection<country> countriescollection     {         { return _countriescollection; }         set         {             _countriescollection = value;             notifypropertychanged("countriescollection");         }     }      private observablecollection<state> _statescollection;      public observablecollection<state> statecollection     {         { return _statescollection; }         set         {             _statescollection = value;             notifypropertychanged("statecollection");         }     }      private country _selectedcountry;      public country selectedcountry     {         { return _selectedcountry; }         set         {             _selectedcountry = value;             if (_selectedcountry != null && _selectedcountry.states != null)             {                 statecollection = new observablecollection<state>(_selectedcountry.states);             }             notifypropertychanged("selectedcountry");         }     }      private void loaddata()     {         if (countriescollection != null)         {             countriescollection.add(new country             {                 countryid = 1,                 countryname = "india",                 states = new list<state>                             {                                     new state { stateid = 1, statename = "gujarat"},                                     new state { stateid = 2, statename = "punjab"},                                     new state { stateid = 3, statename = "maharastra"}                             }             });             countriescollection.add(new country             {                 countryid = 2,                 countryname = "chine",                 states = new list<state>                             {                                     new state { stateid = 4, statename = "chine_state1"},                                     new state { stateid = 5, statename = "chine_state2"},                                     new state { stateid = 6, statename = "chine_state3"}                             }             });             countriescollection.add(new country             {                 countryid = 3,                 countryname = "japan",                 states = new list<state>                             {                                     new state { stateid = 7, statename = "japan_state1"},                                     new state { stateid = 8, statename = "japan_state2"},                                     new state { stateid = 9, statename = "japan_state3"}                             }             });         }     }      public event propertychangedeventhandler propertychanged;      private void notifypropertychanged(string info)     {         propertychanged?.invoke(this, new propertychangedeventargs(info));     }  } 

xalm :

 <stackpanel orientation="horizontal" >     <datagrid autogeneratecolumns="true"                height="300" width="300"                horizontalalignment="left" margin="30"                itemssource="{binding countriescollection}"                selecteditem="{binding selectedcountry}">      </datagrid>     <datagrid autogeneratecolumns="true"                height="300" width="300"                horizontalalignment="left" margin="30"                 itemssource="{binding selectedcountry.states}">      </datagrid> </stackpanel> 

here have autogeneratecolumns property of datagrid have change per requirement.

i hope sample code make things easy understand you.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -