c# - Bound DataGrid ItemSource retuen null or blank objects -


my datagrid's itemsource returns empty objects or null. can case ? properties bound. while adding data, shows proper on grid, while retrieving gives null.

xml

  <datagrid autogeneratecolumns="false" grid.row="2" height="208" horizontalalignment="left" margin="20,71,0,0" name="dgvwell" verticalalignment="top" width="528" horizontalscrollbarvisibility="visible" verticalscrollbarvisibility="visible" borderbrush="#ffb7b39d" background="lightyellow" rowbackground="lightgray" alternatingrowbackground="#fffffff5" borderthickness="10" canuserreordercolumns="false" canusersortcolumns="false" fontsize="13" canuseraddrows="false">       <datagrid.columns>           <datagridtextcolumn header="layer name" width="80" binding="{binding layername}"/>           <datagridtextcolumn width="100" binding="{binding porosity}">           <datagridtextcolumn.header>               <grid width="100">                   <grid.rowdefinitions>                       <rowdefinition />                       <rowdefinition/>                   </grid.rowdefinitions>                   <textblock text="porosity" grid.row="0"/>                   <combobox grid.row="1" width="70" horizontalalignment="center" name="cboporosity">                       <comboboxitem content="pascal" isselected="true" />                       <comboboxitem content="psi"/>                       <comboboxitem content="bar"/>                       <comboboxitem content="barye"/>                   </combobox>               </grid>           </datagridtextcolumn.header>           </datagridtextcolumn>                <datagridtextcolumn header="permeability" binding="{binding permeability}"/>           <datagridtextcolumn width="120" binding="{binding path= perforationstartdepth,mode=twoway}" clipboardcontentbinding="{binding perforationstartdepth}">               <datagridtextcolumn.header>                    <grid>                       <grid.rowdefinitions>                           <rowdefinition height="25"/>                           <rowdefinition/>                       </grid.rowdefinitions>                       <textblock text="perforation start" grid.row="0"/>                       <combobox grid.row="1" width="60" name="cboperfstart">                           <comboboxitem content="ft" isselected="true" />                           <comboboxitem content="m"/>                           <comboboxitem content="cm"/>                       </combobox>                   </grid>                </datagridtextcolumn.header>           </datagridtextcolumn>           <datagridtextcolumn width="145"  binding="{binding perforationenddepth}">               <datagridtextcolumn.header>                    <grid>                       <grid.rowdefinitions>                           <rowdefinition height="25"/>                           <rowdefinition/>                       </grid.rowdefinitions>                       <textblock text="perforation end" grid.row="0"/>                       <combobox grid.row="1" width="60" name="cboperfend">                           <comboboxitem content="ft" isselected="true"/>                           <comboboxitem content="m"/>                           <comboboxitem content="cm"/>                       </combobox>                   </grid>                </datagridtextcolumn.header>           </datagridtextcolumn>            <datagridtextcolumn width="140"  binding="{binding reservoirpressure,mode=twoway}">               <datagridtextcolumn.header>                                                       <grid>                       <grid.rowdefinitions>                           <rowdefinition height="25"/>                           <rowdefinition/>                       </grid.rowdefinitions>                       <textblock text="reservoir pressure" grid.row="0"/>                       <combobox grid.row="1" width="60" name="cborespress">                           <comboboxitem content="pascal" isselected="true" />                           <comboboxitem content="psi"/>                           <comboboxitem content="bar"/>                           <comboboxitem content="barye"/>                       </combobox>                   </grid>               </datagridtextcolumn.header>                                          </datagridtextcolumn>           <datagridtextcolumn width="145" header="water cut" binding="{binding watercut}"></datagridtextcolumn>       </datagrid.columns> </datagrid> 

the backend object:

public struct step2data {     public string layername { get; set; }     public int porosity { get; set; }     public int permeability { get; set; }     public int perforationstartdepth { get; set; }     public int perforationenddepth { get; set; }     public int reservoirpressure { get; set; }     public int watercut { get; set; } }   list<step2data> step2datas = dgvwell.itemssource list<step2data>; 

the above retrieval returns null.

can me know, why grid doesn't return proper itemsource.

edit

based on eg helped me implemented code. in view, have combobox that's lets user select number. based on number, many number of rows added grid. tried work on it, doesn't show rows :

in model class:

public observablecollection<step2model> step2modellist  {     {         if (step2modellist == null)             step2modellist = new observablecollection<step2model>();          return step2modellist;     }     set     {         step2modellist = value;         changed("step2modellist");     } }  public void addstep2modeltolist(step2model step2model) {     step2modellist.add(step2model); }  public void addrowstolist(int count) {     (int = 0; < count; i++)     {         step2modellist.add(new step2model());  // updated         /*         additemcommand = new actioncommand         {             executedelegate = o => step2modellist.add(new step2model())         };         */     } } 

datagrid bound step2modellist

itemssource="{binding step2modellist, mode=twoway}"> 

on combo box selection,

private void cbonumzones_selectionchanged(object sender, selectionchangedeventargs e) {     int num = (int)cbonumzones.selecteditem;     console.writeline("numer of zones selected = " + num);     if (num > 0)     {         step2infodata st = this.datacontext step2infodata;         st.addrowstolist(count);     } } 

after grid doesn't reflect 2 added rows in step2modellist.

i think, don't understand concept of datagrid.itemsource. read http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.itemssource(v=vs.95).aspx

datagrid.itemsource used populate datagrid not other way around.

if want access datagrid items use:

dgvwell.items; 

update, observablecollection sample:

viewmodel:

public class datagridsampleviewmodel {     public observablecollection<step2data> data { get; set; }     public icommand additemcommand { get; set; }      public datagridsampleviewmodel()     {         data = new observablecollection<step2data>();          additemcommand = new actioncommand         {             executedelegate = o => data.add(new step2data { layername = datetime.now.ticks.tostring() })         };     } } 

view code behind:

public partial class datagridsampleview : window {     public datagridsampleview()     {         initializecomponent();         this.datacontext = new datagridsampleviewmodel();     } } 

view:

<window x:class="simplemvvmapp.datagridsampleview"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"          xmlns:simplemvvmapp="clr-namespace:simplemvvmapp"         mc:ignorable="d"         title="datagridsampleview" height="300" width="300"          d:datacontext="{d:designinstance simplemvvmapp:datagridsampleviewmodel}">     <stackpanel>         <button content="add new item" command="{binding additemcommand}" />         <listbox itemssource="{binding data}">             <listbox.itemtemplate>                 <datatemplate>                     <textblock text="{binding layername}" />                 </datatemplate>             </listbox.itemtemplate>         </listbox>     </stackpanel> </window> 

full source code may found @ https://github.com/mswietlicki/simplemvvmapp


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -