sql server - How to filter displayed products according to their category in C# -
this question has answer here:
okay. after several hours of thinking decided ask question again since couldn't make work though had guidance answers in previous question. here's original question
now i'm creating point of sale system , want display products dynamically in database buttons on tabcontrol - tabpages according producttype. problem is, products tabpags buttons can't sort them out each tabpage according producttype same list of products display on every tabpage below.
and database.
private void createtabpages() // create tab pages each producttype { con.open(); sqldataadapter sda = new sqldataadapter("select distinct producttype, description tblproducttype", con); datatable dt = new datatable(); sda.fill(dt); foreach (datarow dr in dt.rows) { tabcontrol1.tabpages.add(dr["producttype"].tostring(),dr["description"].tostring()); } con.close(); } private void addproductstotabbedpanel() // add products tab pages { foreach (tabpage tp in tabcontrol1.tabpages) { con.open(); sqldataadapter sda = new sqldataadapter("select distinct description tblproduct", con); datatable dt = new datatable(); sda.fill(dt); flowlayoutpanel flp = new flowlayoutpanel(); flp.dock = dockstyle.fill; foreach (datarow dr in dt.rows) { button b = new button(); b.size = new size(100, 100); b.text = dr["description"].tostring(); flp.controls.add(b); } tp.controls.add(flp); con.close(); } }
i appreciate feedback give me on this.
disclaimer: yes, know op's pictures show winforms, in the other question op stated following tutorial on youtube
. thought i'd provide way represent same data using wpf.
you use model
display each of datatypes
, instead of handling raw data
; have @ object-relational mapping, entity-framework, nhibernate, etc.
create product
model hold data product
table.
public class product { public int productid {get;set;} public int producttype { get; set; } public string description { get; set; } public double price { get; set; } public byte[] image { get; set; } }
create product type
model hold data product type
table.
public class producttype { public int producttypeid { get; set; } public string description { get; set; } }
create window
/usercontrol
hold tabcontrol
.
<window.resources> <local:productconverter x:key="productconverter" /> <local:producttypeconverter x:key="producttypeconverter" /> </window.resources> <tabcontrol itemssource="{binding myproducts, converter={staticresource productconverter}}"> <tabcontrol.itemtemplate> <datatemplate> <textblock> <textblock.text> <multibinding converter="{staticresource producttypeconverter}"> <binding path="producttype"/> <binding path="datacontext.myproducttypes" relativesource="{relativesource ancestortype={x:type window}}"/> </multibinding> </textblock.text> </textblock> </datatemplate> </tabcontrol.itemtemplate> <tabcontrol.contenttemplate> <datatemplate> <listbox itemssource="{binding}"> <listbox.itemtemplate> <datatemplate> <button width="150" content="{binding description}"/> </datatemplate> </listbox.itemtemplate> </listbox> </datatemplate> </tabcontrol.contenttemplate> </tabcontrol>
using itemtemplate
, contenttemplate
ensure product
objects have same format , styling.
this converter converts entire list of products
groups of products
based on product type
value.
public class productconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { if(value list<product> != null) { return (value list<product>).groupby(a => new { a.producttype }); } return null; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { return null; } }
this converter converts product.producttype
value producttype.description
public class producttypeconverter : imultivalueconverter { public object convert(object[] values, type targettype, object parameter, cultureinfo culture) { if(values[0] != null && values[0] != dependencyproperty.unsetvalue && values[1] != null && values[1] != dependencyproperty.unsetvalue) { string f= (values[1] list<producttype>) .where(a => a.producttypeid.equals(values[0])) .first().description; return f; } return false; } public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture) { return null; } }
create properties using in displaying tabcontrol.items
.
public list<product> myproducts { get; set; } public list<producttype> myproducttypes { get; set; }
afterwards, have represent raw data
in model format. (my sql bit iffy)
sqlcommand sqlcmdproducts = new sqlcommand("select * tblproduct", myconnection); sqldatareader productreader = sqlcmdproducts.executereader(); while (productreader.read()) { myproducttypes.add(new producttype() { producttypeid = int32.parse(productreader["producttype"].tostring()), description = int32.parse(productreader["description"].tostring()), }; } sqlcommand sqlcmdproducttype = new sqlcommand("select * tblproducttype", myconnection); sqldatareader producttypereader = sqlcmdproducttype.executereader(); while (producttypereader.read()) { myproducts.add(new product() { productid = int32.parse(producttypereader["productid"].tostring()), producttype = int32.parse(producttypereader["producttype"].tostring()), description = producttypereader["description"].tostring()), price = double.parse(producttypereader["price"].tostring()), }; }
Comments
Post a Comment