Mohammad Sherif
Mohammad Sherif

Reputation: 199

DatagridComboboxColumn Binding in WPF

I am new to WPF Programming.

I have a datagrid with 2 columns. 1st column is a dataGridTextColumn and 2nd column is DataGridComboboxColumn.

I have 2 values coming from database. One value i want to show in datagrid 1st column and 2nd value i want to use it as selected item/value of comboboxcolumn.

the values to be added to combobox are static, that is 0 to 9.

XAML
<DataGrid x:Name="dg_phase_details" RowHeaderWidth="0" SelectionMode="Single" CellStyle="{StaticResource Body_Content_DataGrid_Centering}" AutoGenerateColumns="False" CanUserAddRows="False" HorizontalAlignment="Left" Margin="380,154,0,0" VerticalAlignment="Top" Height="320" Width="330" AlternationCount="2" AlternatingRowBackground="LightGray" RowHeight="30" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="Phase_Name" IsReadOnly="True" Header="Phases" Binding="{Binding Phase}" Width="*" ElementStyle="{StaticResource dg_Margin_left}"/>
            <DataGridComboBoxColumn x:Name="Combo_Imp_Value" Header="Importance" Width="*"  />
        </DataGrid.Columns>
    </DataGrid>

c#
ObservableCollection<string> list_PhaseVal = new ObservableCollection<string>() { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Combo_Imp_Value.ItemsSource = list_PhaseVal;

It shows a combobox on 2nd column of grid but without values.

How can i do it?

Upvotes: 0

Views: 4746

Answers (2)

mm8
mm8

Reputation: 169390

If your DataGrid's ItemsSource property is set to a DataView of a DataTable this DataTable should have a column that stores the selected value of the ComboBox. If this column is named "YourColumn" you could bind the SelectedItem property of the ComboBox to it like this:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding YourColumn}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox x:Name="myCmb" Loaded="myCmb_Loaded" SelectedItem="{Binding YourColumn}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

If there is no such column in the DataTable you should add it:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("YourColumn"));
dg_phase_details.ItemsSource = dt.DefaultView;

The selected value must be stored somewhere.

Upvotes: 1

Tk1993
Tk1993

Reputation: 521

Create a ComboBoxColumn in the code and bind items to it dynamically. Below is a sample code. Hope it helps

DataGridComboBoxColumn combo = new DataGridComboBoxColumn();
string[] datasource = { "0", "1","2","3","4","5","6","7","8","9"};
combo.ItemsSource= datasource;
dataGrid1.Columns.Add(combo);

UPDATE

Some new updates as according to your updated problem statement.(If any issues please comment)

Replace the DataGridComboBoxColumn like this:

<DataGridTemplateColumn>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="myCmb" Loaded="myCmb_Loaded" SelectedItem="{Binding Value}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

In CS

  private void myCmb_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;
            ObservableCollection<string> list_PhaseVal = new ObservableCollection<string>() { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            cmb.ItemsSource = list_PhaseVal;
        }

Upvotes: 1

Related Questions