c# - How to add empty row to the DataGrid? -
i need allow user add information directly in datagrid put "canuseraddrows" property not work, appears follows:
this datagrid:
<datagrid x:name="dtgpersons" grid.row="3" canuseraddrows="true"> <datagrid.columns> <datagridtextcolumn header="n°" width="*" /> <datagridtextcolumn header="name" width="*" /> <datagridtextcolumn header="carrer" width="*" /> <datagridtextcolumn header="group" width="*" /> <datagridtextcolumn header="age" width="*" /> </datagrid.columns> </datagrid>
edit
this new grid code:
<datagrid x:name="dtgperson" grid.row="3" itemssource="{binding lstperson}" autogeneratecolumns="false" canuseraddrows="true"> <datagrid.columns> <datagridtextcolumn header="n°" width="*" binding="{binding no}" /> <datagridtextcolumn header="name" width="*" binding="{binding name}" /> <datagridtextcolumn header="carrer" width="*" binding="{binding carrer}" /> <datagridtextcolumn header="group" width="*" binding="{binding group}" /> <datagridtextcolumn header="age" width="*" binding="{binding age}" /> </datagrid.columns> </datagrid>
and code behind: observablecollection:
public observablecollection<person> lstperson { get; set; }
my mainwindow
public mainwindow() { initializecomponent(); datacontext = this; lstperson = new observablecollection<person>(); }
my person class
public class person { public int no { get; set; } public string name { get; set; } public string carrer { get; set; } public string group { get; set; } public int age { get; set; } }
1) change grid xaml
<datagrid itemssource="{binding gridcollection}" canuseraddrows="true" autogeneratecolumns="false"> <datagrid.columns> <datagridtextcolumn header="n°" binding="{binding number}" width="*" /> <datagridtextcolumn header="name" binding="{binding name}" width="*" /> <datagridtextcolumn header="carrer" binding="{binding career}" width="*" /> <datagridtextcolumn header="group" binding="{binding group}" width="*" /> <datagridtextcolumn header="age" binding="{binding age}" width="*" /> </datagrid.columns> </datagrid>
2) define collection in viewmodel or codebehind
public observablecollection<griddatamodel> gridcollection { get; set; }
3) init collection before use
public mainwindow() { initializecomponent(); datacontext = this; gridcollection = new observablecollection<griddatamodel>(); }
4) after this, can add items in grid via gui , store in collection
Comments
Post a Comment