c# - Binding DataTable object with ListBox in WPF vs Binding DataTable object with ListBox in Windows Form Application -


i learned in windows forms application following youtube tutorial i'm having hard time implementing same approach in wpf.

private void populatenames() {     using (connection = new sqlconnection(connection_string))     using (sqldataadapter adapter = new sqldataadapter("select * namestable", connection))     {         datatable namestable = new datatable();         adapter.fill(namestable);         debug.write(namestable.asdataview());          nameslistbox.displaymemberpath = "name";         nameslistbox.selectedvaluepath = "id";         nameslistbox.itemssource = namestable.asdataview();     } } 

and xaml listview is

<grid>     <listbox itemssource="{binding}" displaymemberpath="name" x:name="nameslistbox"               horizontalalignment="left" height="209" margin="90,59,0,0"               verticalalignment="top" width="341" selectionchanged="listbox_selectionchanged"/> </grid> 

and cannot understand wrong followed msdn , many questions on stackoverflow since i'm newbie maybe didn't answer looking for.

so question how data binding different in wpf , windows form applications thought implementation in winforms application pretty straight forward, cannot figure out how populate listbox entries database.

your code has not problem , should works. missed calling populatenames method.

also here don't need itemssource="{binding}" , displaymemberpath="name" in xaml because have set in code behind.

if want in xaml this:

private void populatenames() {     using (connection = new sqlconnection(connection_string))     using (sqldataadapter adapter = new sqldataadapter("select * namestable", connection))     {         datatable namestable = new datatable();         adapter.fill(namestable);         debug.write(namestable.asdataview());         datacontext = namestable.asdataview();     } } 

and in xaml:

<listbox itemssource="{binding}" displaymemberpath="name" selectedvaluepath="id"           x:name="nameslistbox" horizontalalignment="left" height="209"           verticalalignment="top" /> 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -