{"id":1508,"date":"2015-02-26T10:41:29","date_gmt":"2015-02-26T09:41:29","guid":{"rendered":"http:\/\/www.matjazev.net\/blog\/?p=1508"},"modified":"2015-02-26T10:42:28","modified_gmt":"2015-02-26T09:42:28","slug":"xaml-datagrid-using-one-datatable-as-lookup","status":"publish","type":"post","link":"https:\/\/www.matjazev.net\/blog\/2015\/02\/26\/xaml-datagrid-using-one-datatable-as-lookup\/","title":{"rendered":"XAML DataGrid using one DataTable as lookup"},"content":{"rendered":"<h2>Problem:<\/h2>\n<p>I have a DataTable <strong>with lookup column into another table<\/strong>. Another table is a small table <strong>so all the records can fit in small combobox<\/strong> and the user wants to be able to select lookup record with combobox.<\/p>\n<h2>Solution:<\/h2>\n<p>XAML has DataGridComboBoxColumn, but it is <strong>difficult to bind<\/strong>, because <strong>DataGridComboBoxColumn can\u0092&#8217;t bind<\/strong> to Windows binding source. It is obviously quite know problem, but I could\u0092n&#8217;t find an easy solution.<\/p>\n<p>Test tables:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nMAIN_TABLE \r\n{ \r\n  ID, \r\n  SubTableID, \r\n  ... other fields ...\r\n}\r\n\r\nSUB_TABLE \r\n{ \r\n  ID, \r\n  Description \r\n}\r\n<\/pre>\n<h3>Naive<\/h3>\n<p>Naive solution, that doesn&#8217;\u0092t work<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;DataGrid ItemsSource=&quot;{Binding mainTable}&quot;&gt;\r\n  &lt;DataGrid.Columns&gt;\r\n    &lt;DataGridTextColumn Header=&quot;ID&quot; \r\n                        Binding=&quot;{Binding ID}&quot; \/&gt;\r\n    &lt;DataGridTextColumn Header=&quot;SubID&quot; \r\n                        Binding=&quot;{Binding SubTableID }&quot; \/&gt;\r\n    &lt;DataGridComboBoxColumn Header=&quot;Desc&quot; \r\n                            ItemsSource=&quot;{Binding subTable}}&quot; &lt;!-- can't bind! --&gt;\r\n                            SelectedValueBinding=&quot;{Binding SubTableID}&quot;\r\n                            SelectedValuePath=&quot;ID&quot; \r\n                            DisplayMemberPath=&quot;Description&quot; \/&gt;\r\n  &lt;\/DataGrid.Columns&gt;            \r\n&lt;\/DataGrid&gt;\r\n<\/pre>\n<p>The solution doesn\u0092&#8217;t work because \u0096 for some reason\u0096<strong> DataGridComboBoxColumn can&#8217;\u0092t bind subTable<\/strong> even if it is defined exactly as mainTable in datasource for the whole xaml window.<\/p>\n<h3>Solution<\/h3>\n<p>The solution is quite easy. Create a resource that binds to sub table and then use that resurce sa data source \ud83d\ude42<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;DataGrid ItemsSource=&quot;{Binding mainTable}&quot;&gt;\r\n  &lt;DataGrid.Resources&gt;\r\n    &lt;CollectionViewSource x:Key=&quot;cvsSubTable&quot; \r\n                          Source=&quot;{Binding subTable }&quot;\/&gt; &lt;!-- can bind! --&gt;\r\n  &lt;\/DataGrid.Resources&gt;\r\n  &lt;DataGrid.Columns&gt;\r\n    &lt;DataGridTextColumn Header=&quot;ID&quot; \r\n                        Binding=&quot;{Binding ID}&quot; \/&gt;\r\n    &lt;DataGridTextColumn Header=&quot;SubID&quot; \r\n                        Binding=&quot;{Binding SubTableID }&quot; \/&gt;\r\n    &lt;DataGridComboBoxColumn Header=&quot;Desc&quot; \r\n                            ItemsSource=&quot;{Binding Source={StaticResource cvsSubTable }}&quot;  &lt;!-- now it is ok! --&gt; \r\n                            SelectedValueBinding=&quot;{Binding SubTableID}&quot;\r\n                            SelectedValuePath=&quot;ID&quot; \r\n                            DisplayMemberPath=&quot;Description&quot; \/&gt;\r\n  &lt;\/DataGrid.Columns&gt;            \r\n&lt;\/DataGrid&gt;         \r\n<\/pre>\n<p>Lost 4 hours on that! Grrrr..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: I have a DataTable with lookup column into another table. Another table is a small table so all the records can fit in small combobox and the user wants to be able to select lookup record with combobox. Solution: XAML has DataGridComboBoxColumn, but it is difficult to bind, because DataGridComboBoxColumn can\u0092&#8217;t bind to Windows &hellip; <a href=\"https:\/\/www.matjazev.net\/blog\/2015\/02\/26\/xaml-datagrid-using-one-datatable-as-lookup\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">XAML DataGrid using one DataTable as lookup<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,34],"tags":[60,59,58],"_links":{"self":[{"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/posts\/1508"}],"collection":[{"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/comments?post=1508"}],"version-history":[{"count":20,"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"predecessor-version":[{"id":1528,"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions\/1528"}],"wp:attachment":[{"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matjazev.net\/blog\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}