none
Populate Combobox in a DataGrid detailrow

    Question

  • I have a datagrid which has an itemssource bound to a datatable defaultview. Through XAML several columns and items of the rowdetails are databound to fields of this datatable. In the rowdetail I have a combobox which text property is also bound to a field of the datatable.

    How can I populate this combobox with items from an other datatable? Respectively I like to bind the combobox property "itemssource" to an other datatable. But I am stucked since I found no way to address this combobox of the detailrow in order to fill its itemslist.

    My best guess is to create a resource in XAML as shown below:

    <List x:Key="weighstack"></List>

     

     

     

    But I found now way to populate this resource programatically. Try to add items through the below listed code behind (VB.NET) failed since the member "additem" for the type "list" has not been found.

     Me.Resources.Item("weighstack").additem("first item")
    

    For me it is hard to believe that I am the only one with this kind of task for an application but I am reading through books, blogs, forums and documentation since 2 days now. Any idea is highly appreciated.

    best regards,

    Michael Lutz

    BITsoft

    Wednesday, December 01, 2010 4:47 PM

Answers

  • Dear Min

    Thank you for your reply and assistence. A couple minutes ago I found a different solution to accomplish my task. In the RowDetailsisibilityChanged event I am setting the necessary properties. Since I am the opinion that this "simple" task is needed in a lot of LOB applications, I post my code below, despite the fact, that it refers to my custom objects. But I think anybody interested in this issue is able to transfer this sample code to the own belongings.

    Code behind (VB.NET):

    Private Sub power_RowDetailsVisibilityChanged(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridRowDetailsEventArgs) Handles power.RowDetailsVisibilityChanged
        Dim cmbweightstack As ComboBox = TryCast(e.DetailsElement.FindName("cmb_WeightStack"), ComboBox)
        cmbweightstack.ItemsSource = cEC.ds.Tables.Item("weightstacks").DefaultView
        cmbweightstack.DisplayMemberPath = "weightstack_header"
        Dim cmbadditionalweight As ComboBox = TryCast(e.DetailsElement.FindName("cmb_Addon_WeightStack"), ComboBox)
        cmbadditionalweight.ItemsSource = cEC.ds.Tables.Item("addon_weightstacks").DefaultView
        cmbadditionalweight.DisplayMemberPath = "addon_weightstack_header"
      End Sub
    

    Explaination: cEC is a class hosting the dataset with the datatables "weightstacks" and "addon_weightstacks" which I bind to the combo boxes as shown in the XAML code below:

    <Label Name="lbl_gv_power_WeightStack" Content="Gewichtsstapel" Grid.Column="3" Grid.Row="1" Margin="0" Padding="2" HorizontalContentAlignment="Center"></Label>
                          <ComboBox Name="cmb_WeightStack" Text="{Binding Path=device_weightstack_header}" Grid.Column="3" Grid.Row="3" Background="White" Height="28" Margin="0" Width="Auto" IsReadOnly="False" VerticalAlignment="Top" />
                          <Label Name="lbl_gv_power_Addon_WeightStack" Content="Zusatzgewichte" Grid.Column="5" Grid.Row="1" Margin="0" Padding="2" HorizontalContentAlignment="Center"></Label>
                          <ComboBox Name="cmb_Addon_WeightStack" Text="{Binding Path=device_addon_weightstack_header}" Grid.Column="5" Grid.Row="3" Background="White" Height="28" Margin="0" Width="Auto" IsReadOnly="False" VerticalAlignment="Top" />
    

    However - I will try the general ideas of Min, too, since my solution seem to have a little delay time when showing the RowDetail of the DataGrid.

    Thanks again, Min. Best regards,

     


    Michael Lutz, BITsoft
    • Marked as answer by BITsoft_mlutz Friday, December 03, 2010 10:55 AM
    Thursday, December 02, 2010 8:51 AM

All replies

  • Hi BITsoft,

    I think you are in the right direction. Generally we bind Combox's ItemsSource to a resource to solve this.

    The only issue you have is that the List object you created as a resource is actually refering to System.Windows.Documents.List class. If you want to create a System.Collections.ArrayList object, you need to specify namespace mapping. We can use the following markup code to create an ArrayList object.

     

    <collection:ArrayList xmlns:collection="clr-namespace:System.Collections;assembly=mscorlib" x:Key="weighstack"/>
    

    Then you can use your original code to populate this ArrayList.

    Please refer to XAML Namespaces and Namespace Mapping for WPF XAML for more information.

    Alternatively, you can also use CollectionViewSource to accomplish this. It's can created an instance of CollectionViewSource as a resource. And bind it to the ComboBox's ItemsSource property. Then you only need to set the Source property of this CollectionViewSource to your datatable. Please refer to CollectionViewSource Class for more information.

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min

    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact
    msdnmg@microsoft.com.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, December 02, 2010 8:06 AM
  • Dear Min

    Thank you for your reply and assistence. A couple minutes ago I found a different solution to accomplish my task. In the RowDetailsisibilityChanged event I am setting the necessary properties. Since I am the opinion that this "simple" task is needed in a lot of LOB applications, I post my code below, despite the fact, that it refers to my custom objects. But I think anybody interested in this issue is able to transfer this sample code to the own belongings.

    Code behind (VB.NET):

    Private Sub power_RowDetailsVisibilityChanged(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridRowDetailsEventArgs) Handles power.RowDetailsVisibilityChanged
        Dim cmbweightstack As ComboBox = TryCast(e.DetailsElement.FindName("cmb_WeightStack"), ComboBox)
        cmbweightstack.ItemsSource = cEC.ds.Tables.Item("weightstacks").DefaultView
        cmbweightstack.DisplayMemberPath = "weightstack_header"
        Dim cmbadditionalweight As ComboBox = TryCast(e.DetailsElement.FindName("cmb_Addon_WeightStack"), ComboBox)
        cmbadditionalweight.ItemsSource = cEC.ds.Tables.Item("addon_weightstacks").DefaultView
        cmbadditionalweight.DisplayMemberPath = "addon_weightstack_header"
      End Sub
    

    Explaination: cEC is a class hosting the dataset with the datatables "weightstacks" and "addon_weightstacks" which I bind to the combo boxes as shown in the XAML code below:

    <Label Name="lbl_gv_power_WeightStack" Content="Gewichtsstapel" Grid.Column="3" Grid.Row="1" Margin="0" Padding="2" HorizontalContentAlignment="Center"></Label>
                          <ComboBox Name="cmb_WeightStack" Text="{Binding Path=device_weightstack_header}" Grid.Column="3" Grid.Row="3" Background="White" Height="28" Margin="0" Width="Auto" IsReadOnly="False" VerticalAlignment="Top" />
                          <Label Name="lbl_gv_power_Addon_WeightStack" Content="Zusatzgewichte" Grid.Column="5" Grid.Row="1" Margin="0" Padding="2" HorizontalContentAlignment="Center"></Label>
                          <ComboBox Name="cmb_Addon_WeightStack" Text="{Binding Path=device_addon_weightstack_header}" Grid.Column="5" Grid.Row="3" Background="White" Height="28" Margin="0" Width="Auto" IsReadOnly="False" VerticalAlignment="Top" />
    

    However - I will try the general ideas of Min, too, since my solution seem to have a little delay time when showing the RowDetail of the DataGrid.

    Thanks again, Min. Best regards,

     


    Michael Lutz, BITsoft
    • Marked as answer by BITsoft_mlutz Friday, December 03, 2010 10:55 AM
    Thursday, December 02, 2010 8:51 AM
  • Hi BITsoft,

    Thank you for your reply and the detailed additional feedback on how you were successful in resolving this issue. I am glad to hear that the problem has been resolved.

    You can also mark your solution as answer to highlight it in the thread. This makes it easier to locate answers when other community members are searching for specific information or solutions.

    Have a nice day!

    Best regards,

    Min

    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact
    msdnmg@microsoft.com.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, December 03, 2010 8:03 AM