c# - Windows Store App writing sqlite query into the listview's textblocks -
i developing windows store application. using sqlite database , want put result of select query listview. listview has grids, , textboxes inside these grids. cannot target these textboxes in c# when gave name them. here code sample project: first sqlite query:
private void page_loaded(object sender, routedeventargs e) { string dbpath = string.empty; dbpath = path.combine(windows.storage.applicationdata.current.localfolder.path, "mydb.s3db"); using (var db= new sqlite.sqliteconnection(dbpath)) { var query= db.table<company>(); var result= query.tolist(); foreach( var x in result ) { company frm= new company(); //addition listview... } } }
everything works above except typing listview. have tested textblocks outside listview.
and xaml:
<listview name="lv" grid.column="1" horizontalalignment="stretch" verticalalignment="stretch" margin="10,10,10,10" grid.row="3" grid.rowspan="11" grid.columnspan="8"> <listview.headertemplate> <datatemplate> <grid loaded="gheader_loaded"> <grid.columndefinitions> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <textblock grid.row="2" grid.column="0" text="company no" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="1" text="company name" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="2" text="phone" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="3" text="fax" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="4" text="address" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="5" text="state" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="6" text="city" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <combobox grid.row="2" grid.column="7" horizontalalignment="center" verticalalignment="center"> <comboboxitem content="test1"></comboboxitem> <comboboxitem content="test2"></comboboxitem> <comboboxitem content="test3"></comboboxitem> <comboboxitem content="test4"></comboboxitem> <comboboxitem content="test5"></comboboxitem> </combobox> </grid> </datatemplate> </listview.headertemplate> <listview.itemtemplate> <datatemplate> <grid loaded="gheader_loaded"> <grid.columndefinitions> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> <columndefinition /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <textblock x:name="test" grid.row="2" grid.column="0" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="1" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="1" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="1" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="2" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="3" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="4" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> <textblock grid.row="2" grid.column="5" text="" fontsize="25" horizontalalignment="center" verticalalignment="center"/> </grid> </datatemplate> </listview.itemtemplate> </listview>
sorry long post, wanted give detailed information program maybe can see went wrong. appreciate help/feedback on this.
my regards...
you need use databinding listview.
first bind collection listview this:
private void page_loaded(object sender, routedeventargs e) { ** snip ** using (var db= new sqlite.sqliteconnection(dbpath)) { var query= db.table<company>(); lv.itemssource = query.tolist(); } }
after can bind inside datatemplate properties on object. let's suppose company-class defined this:
public class company { public string name{get; set;} }
then can inside itemtemplate:
<listview.itemtemplate> <datatemplate> <grid loaded="gheader_loaded"> <grid.columndefinitions> ** snip ** </grid.columndefinitions> <grid.rowdefinitions> ** snip ** </grid.rowdefinitions> <textblock grid.row="2" grid.column="0" text="{binding name}" /> ** snip ** </grid> </datatemplate> </listview.itemtemplate>
when rendering each item, @ current instance (of type company
), extract name
-property , put value of textblock
.
Comments
Post a Comment