wpf - How can I add sorting by clicking on the column header in this GridView? -


ipmo wpk  $connectionstring = $connectionstring = "server=localhost;integrated security=true" $conn = new-object system.data.sqlclient.sqlconnection $conn.connectionstring = $connectionstring  $conn.open()   function invoke-sql1 {     param( [string]$sql,            [system.data.sqlclient.sqlconnection]$connection            )     $cmd = new-object system.data.sqlclient.sqlcommand($sql,$connection)     $ds = new-object system.data.dataset     $da = new-object system.data.sqlclient.sqldataadapter($cmd)     $da.fill($ds) | out-null     return $ds.tables[0] }  function show-bockmarks ($conn) {         new-listview -name listview -view {            new-gridview -allowscolumnreorder -columns {                new-gridviewcolumn "title"             }     } -show -on_loaded {             $ff_sql = @" select 'abc' title union select 'xyz' title union select 'efg' title "@             $tableview = $window | get-childcontrol listview             $tableview.itemssource = @(invoke-sql1 -sql $ff_sql -connection $conn)              }  }  show-bockmarks $conn 

edit: transformed code xaml

ipmo wpk  $connectionstring = $connectionstring = "server=localhost;integrated security=true" $conn = new-object system.data.sqlclient.sqlconnection $conn.connectionstring = $connectionstring  $conn.open()   function invoke-sql1 {     param( [string]$sql,            [system.data.sqlclient.sqlconnection]$connection            )     $cmd = new-object system.data.sqlclient.sqlcommand($sql,$connection)     $ds = new-object system.data.dataset     $da = new-object system.data.sqlclient.sqldataadapter($cmd)     $da.fill($ds) | out-null     return $ds.tables[0] }  function show-bockmarks ($conn) {  [xml] $xaml = @" <window   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   title="mainwindow" > <listview itemssource="{binding persons}"       issynchronizedwithcurrentitem="true"       name="listview">     <listview.view>         <gridview>             <gridview.columns>                 <gridviewcolumn header="title"                                 displaymemberbinding="{binding title}"                                  />                 <gridviewcolumn header="itemid"                                 displaymemberbinding="{binding itemid}"                                  />             </gridview.columns>         </gridview>     </listview.view> </listview> </window> "@ $reader=(new-object system.xml.xmlnodereader $xaml) $form=[windows.markup.xamlreader]::load( $reader )  $ff_sql = @" select 'abc' title, 3 itemid union select 'xyz' title, 2 itemid union select 'efg' title, 1 itemid "@  $tableview = $form.findname("listview") $tableview.itemssource = @(invoke-sql1 -sql $ff_sql -connection $conn)  $form.showdialog() #| out-null }  show-bockmarks $conn 

but when added lines proposed thomas levesque

ipmo wpk  $connectionstring = $connectionstring = "server=localhost;integrated security=true" $conn = new-object system.data.sqlclient.sqlconnection $conn.connectionstring = $connectionstring  $conn.open()   function invoke-sql1 {     param( [string]$sql,            [system.data.sqlclient.sqlconnection]$connection            )     $cmd = new-object system.data.sqlclient.sqlcommand($sql,$connection)     $ds = new-object system.data.dataset     $da = new-object system.data.sqlclient.sqldataadapter($cmd)     $da.fill($ds) | out-null     return $ds.tables[0] }  function show-bockmarks ($conn) {  [xml] $xaml = @" <window   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:util="clr-namespace:thenamespace;assembly=theassembly"   title="mainwindow" > <listview itemssource="{binding persons}"       issynchronizedwithcurrentitem="true"        util:gridviewsort.autosort="true"       name="listview">     <listview.view>         <gridview>             <gridview.columns>                 <gridviewcolumn header="title"                                 displaymemberbinding="{binding title}"                                 util:gridviewsort.propertyname="title"                                  />                 <gridviewcolumn header="itemid"                                 displaymemberbinding="{binding itemid}"                                  util:gridviewsort.propertyname="itemid"                                  />             </gridview.columns>         </gridview>     </listview.view> </listview> </window> "@ $reader=(new-object system.xml.xmlnodereader $xaml) $form=[windows.markup.xamlreader]::load( $reader )  $ff_sql = @" select 'abc' title, 3 itemid union select 'xyz' title, 2 itemid union select 'efg' title, 1 itemid "@  $tableview = $form.findname("listview") $tableview.itemssource = @(invoke-sql1 -sql $ff_sql -connection $conn)  $form.showdialog() #| out-null }  show-bockmarks $conn 

i error

exception calling "load" "1" argument(s): "the property 'gridviewsort.autosort' not exist in xml namespace 'clr-namespace:thenamespace;assembly=theassembly'. line '0'  position '0'." 

i guess have register assembly.

see this blog post (and this one) xaml solution

you can use solution in code using gridviewsort.setautosort , gridviewsort.setpropertyname method. don't know powershell syntax, here in c#:

gridviewsort.setautosort(tableview, true); gridviewsort.setpropertyname(titlecolumn, "title"); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -