c# - How to fill datagrid from list? -
yo guyz,
i'm trying fill 2 datagrids columns tree other list. know how can this? solution read data table , show in message boxes loop. don't know how bind columns showing information in datagrid.
public void wybierzdoraportu() { list<string> nametab = new list<string>(); list<string> numertab = new list<string>(); list<string> ilosctab = new list<string>(); datatable dt = new datatable(); dataset ds = new dataset(); mysqlcommand cmd = new mysqlcommand(" select c.nazwa, c.symbol, z.id_czesci_symbol, z.ilosc, z.z_numer_naprawy `sylpo_test`.`zamowienie` z left join `test`.`czesc` c on c.symbol=z.id_czesci_symbol z.z_numer_naprawy='" + numberbox.content.tostring() + "' order ilosc;", connection); mysqldataadapter adp = new mysqldataadapter(cmd); try { connection.open(); using (mysqldatareader dr = cmd.executereader()) //do query { dt.load(dr); } adp.fill(ds, "loaddatabinding"); } catch (mysqlexception ex) { messagebox.show(ex.tostring()); } { connection.close(); } foreach (datarow drow in dt.rows) { nametab.add(drow[0].tostring()); numertab.add(drow[2].tostring()); ilosctab.add(drow[3].tostring()); } (int = 0; <= dt.rows.count - 6; i++) { czescitable.itemssource = nametab; // not working :/ messagebox.show(nametab.elementat(i) + " " + numertab.elementat(i) + " " + ilosctab.elementat(i)); } (int = 5; <= dt.rows.count - 1; i++) { messagebox.show(nametab.elementat(i) + " " + numertab.elementat(i) + " " + ilosctab.elementat(i)+" druga tura"); } }
and xaml
<datagrid x:name="czescitable" itemssource="{binding}" autogeneratecolumns="false" canuseraddrows="false" horizontalalignment="right" margin="0,207,175,0" verticalalignment="top" width="183" height="66" fontsize="5" fontweight="bold" borderbrush="black" background="white" headersvisibility="column" canuserreordercolumns="false" canusersortcolumns="false" scrollviewer.cancontentscroll="false" verticalscrollbarvisibility="disabled"> <datagrid.columns> <datagridtextcolumn binding="{binding path=nazwatab}" header="nazwa" width="70" isreadonly="true" canuserresize="false" /> <datagridtextcolumn binding="{binding path=numertab}" header="kod" width="60" isreadonly="true" canuserresize="false" /> <datagridtextcolumn binding="{binding path=ilosctab}" header="iloŚĆ (w szt.)" width="51" isreadonly="true" canuserresize="false" /> </datagrid.columns> </datagrid>
thanks help.
first of all, not public property, it's defined inside method or constructor have there :
list<string> nametab = new list<string>();
make public , change observablecollection<string>
.
public observablecollection<string> nametab { { return (observablecollection<string>)getvalue(nametabproperty); } set { setvalue(nametabproperty, value); } } // using dependencyproperty backing store nametab. enables animation, styling, binding, etc... public static readonly dependencyproperty nametabproperty = dependencyproperty.register("nametab", typeof(observablecollection<string>), typeof(mainwindow), new propertymetadata(null));
set datacontext
in constructor or somewhere , set nametab binding source datagrid
.
you should same thing others 2 : numertab , ilosctab .
if don't want use dependencyproperty, try implement inpc interface:
public class itemcontainer : inotifypropertychanged { private observablecollection<string> _nametab; public observablecollection<string> nametab { { return _nametab; } set { _nametab = value; propertychanged(this, new propertychangedeventargs("nametab")); } } public event propertychangedeventhandler propertychanged = delegate { }; }
update 1:
an example of how datagrid should work:
<window x:class="datagridexample.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <datagrid x:name="czescitable" itemssource="{binding nametab}" autogeneratecolumns="false" canuseraddrows="false" verticalalignment="top" fontsize="12" fontweight="bold" borderbrush="black" background="white" headersvisibility="column" canuserreordercolumns="false" canusersortcolumns="false" scrollviewer.cancontentscroll="false" verticalscrollbarvisibility="disabled"> <datagrid.columns> <datagridtextcolumn binding="{binding path=id_czesci_symbol}" header="id" width="51" isreadonly="true" canuserresize="false" /> <datagridtextcolumn binding="{binding path=nazwa}" header="nazwa" width="70" isreadonly="true" canuserresize="false" /> <datagridtextcolumn binding="{binding path=symbol}" header="symbol" width="60" isreadonly="true" canuserresize="false" /> </datagrid.columns> </datagrid> </grid>
codebehind:
public partial class mainwindow : window { public observablecollection<model> nametab { get; set; } public mainwindow() { initializecomponent(); nametab = new observablecollection<model>(); nametab.add(new model() { id_czesci_symbol = 1, nazwa = "nazwa1", symbol = "symbol1" }); nametab.add(new model() { id_czesci_symbol = 2, nazwa = "nazwa2", symbol = "symbol2" }); nametab.add(new model() { id_czesci_symbol = 3, nazwa = "nazwa3", symbol = "symbol3" }); this.datacontext = this; } }
and model:
public class model : inotifypropertychanged { private string _nazwa; public string nazwa { { return _nazwa; } set { _nazwa = value; propertychanged(this, new propertychangedeventargs("nazwa")); } } private string _symbol; public string symbol { { return _symbol; } set { _symbol = value; propertychanged(this, new propertychangedeventargs("symbol")); } } private int _id_czesci_symbol; public int id_czesci_symbol { { return _id_czesci_symbol; } set { _id_czesci_symbol = value; propertychanged(this, new propertychangedeventargs("id_czesci_symbol")); } } public event propertychangedeventhandler propertychanged = delegate { }; }
result:
Comments
Post a Comment