locked
show combobox in datagrid RRS feed

  • Question

  • Hi.

    how can i show a combox in a datagrid?

    For example i have 2 field. field1 is ID and filed 2 is Numbers.

    for each ID i have more than 1 number.

    I Want to show  repetitive IDs in a textbox in one record and in second filed i want to show a combox to show all numbers of that ID.

    How can id do that?

    Wednesday, July 17, 2013 2:11 PM

Answers

  • You can use a DataGridComboBoxColumn to display a ComboBox in a DataGrid: http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx

    For further assistance, could you please describe the data you have in the collection that is bound to the DataGrid? Does the Numbers property contain a collection with different numbers?

    If the type of items in the bound collection has the following three properties; ID (int), SelectedNumber (int) and Numbers (IList<int>), you could use the following markup for the DataGridComboBoxColumn:

    <DataGridComboBoxColumn SelectedValueBinding="{Binding SelectedNumber}">
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Numbers}" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Numbers}" />
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{SelectedNumber}"/>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                    </DataGridComboBoxColumn>

    Note that the DataGridComboBoxColumn has two styles. One for when the cell is not in editing mode and one where the cell is in editing mode. The non-editing mode simply displays a textblock with the currently selected value and once you start editing the cell the textblock is replaced with a ComboBox.

    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:10 AM
    Wednesday, July 17, 2013 4:23 PM

All replies

  • Hi

    Try using this http://msdn.microsoft.com/en-us/library/bb288032.aspx

    Wednesday, July 17, 2013 2:56 PM
  • You can use a DataGridComboBoxColumn to display a ComboBox in a DataGrid: http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx

    For further assistance, could you please describe the data you have in the collection that is bound to the DataGrid? Does the Numbers property contain a collection with different numbers?

    If the type of items in the bound collection has the following three properties; ID (int), SelectedNumber (int) and Numbers (IList<int>), you could use the following markup for the DataGridComboBoxColumn:

    <DataGridComboBoxColumn SelectedValueBinding="{Binding SelectedNumber}">
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Numbers}" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Numbers}" />
                                <Setter Property="ItemTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{SelectedNumber}"/>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                    </DataGridComboBoxColumn>

    Note that the DataGridComboBoxColumn has two styles. One for when the cell is not in editing mode and one where the cell is in editing mode. The non-editing mode simply displays a textblock with the currently selected value and once you start editing the cell the textblock is replaced with a ComboBox.

    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:10 AM
    Wednesday, July 17, 2013 4:23 PM