0

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?

Mohammad Sherif
  • 199
  • 2
  • 3
  • 11
  • 1
    Have you written any code/ any attempt to try to code it first? There are many examples to get you started if its from scratch, e.g. http://stackoverflow.com/questions/5409259/binding-itemssource-of-a-comboboxcolumn-in-wpf-datagrid – Keyur PATEL Feb 08 '17 at 06:28
  • 2
    Give a piece of XAML or C# code so that anyone can give a solution – Tk1993 Feb 08 '17 at 06:37

2 Answers2

1

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;
        }
Tk1993
  • 501
  • 8
  • 21
  • This is already done.. I have updated the question. Please refer it and give me your valuable suggestions. – Mohammad Sherif Feb 08 '17 at 07:01
  • @MohammadSherif How you are binding your DataGrid "dg_phase_details", What is the ItemsSource? – Tk1993 Feb 08 '17 at 07:15
  • ds_Phases = DbHelper.getData("select phase.Phase, details.Id, details.Imp_Value from Phases as phase, Stake_Mem_Details as details where details.Phase_Id = phase.Id and details.Stake_Mem_Id = 1"); dg_phase_details.ItemsSource = new DataView(ds_Phases.Tables[0]); ds_phases is a dataset dg_phase_details is datagrid – Mohammad Sherif Feb 08 '17 at 07:17
  • Yes... Now it shows the value in combobox when i double click on the cell. But when i select an option and go to next row, it give me the error - Two-way binding requires Path or XPath. – Mohammad Sherif Feb 08 '17 at 08:00
  • What is the ItemsSource property of the *DataGrid* set to? – mm8 Feb 08 '17 at 10:42
  • It is Dataset. data coming from the database. – Mohammad Sherif Feb 08 '17 at 11:00
  • See my answer. You need to have a column in your DataTable that stores the selected value in the ComboBox. – mm8 Feb 08 '17 at 11:38
  • 1
    @Tushar Kukreti Thanx. It is working fine now. As i have changed from SelectedItem="{Binding Value}" to SelectedIndex="{Binding Imp_Value}" and rather than using a list i directly put values for combobox items in xml. – Mohammad Sherif Feb 08 '17 at 12:02
1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88