0
votes

I have a problem with wpf datagrid combobox . I have the following code in which items are not getting populated in combobox please help me out if any one done this in wpf toolkit datagrid (NOT in Infragistics) and also let me know how to make that combobox column as editable one ?

  <DataGrid Name="dataGridResultsAdded" AutoGenerateColumns="False" FontWeight="Normal" IsReadOnly="True" ItemsSource="{Binding UserResults,Mode=Default}" SelectedIndex="{Binding SelectedIndexUserResults}" SelectionMode="Single" Margin="0,0,0,0" Height="178" GridLinesVisibility="None">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="RFC ID" Binding="{Binding RFCID}"></DataGridTextColumn>
                    <DataGridTextColumn Header="RFC Title" Binding="{Binding RFCTitle}"></DataGridTextColumn>
                    <DataGridTextColumn Header="RFC Revision" Binding="{Binding RFCRevision}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Trigger Association" Binding="{Binding TriggerAssociation}"></DataGridTextColumn>
                    <DataGridTemplateColumn Header="OrderStatus1" IsReadOnly="False">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox IsEditable="True" ItemsSource="{Binding Path=DataContext.OrderStatus,
                                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"                                           SelectedItem="{Binding Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                    <DataGridComboBoxColumn IsReadOnly="False" Header="OrderStatus"  SelectedItemBinding="{Binding Status,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding OrderStatus, Mode=TwoWay}" />
                    <DataGridTextColumn Header="Status" Binding="{Binding TriggerStatus}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>

private List orderStatus = default(List);

    public List<string> OrderStatus
    {
        get
        {
            if (orderStatus == null)
            {
                orderStatus = new List<string>();
                orderStatus.Add("None");
                orderStatus.Add("New");
                orderStatus.Add("Processing");
                orderStatus.Add("Shipped");
            }
            return orderStatus;
        }
        set
        {
            orderStatus = value;
            NotifyPropertyChanged("OrderStatus");
        }
    }

` public DataTable dtUserResults = default(DataTable);

    public DataTable UserResults
    {
        get
        {
            return dtUserResults;
        }
        set
        {
            dtUserResults = value;
            NotifyPropertyChanged("UserResults");
        }
    }

`View Model Code Below

    public void AddExecute()
    {
        InfoHandler.LogInfo("Entering AddExecute");
        try
        {
            DataTable dtUserRes = new DataTable();
            DataColumn dColumn = default(DataColumn);
            DataRow dRow = default(DataRow);

            DataRow dRowSelected = dtSearchResults.NewRow();
            dRowSelected = dtSearchResults.Rows[intSelectedIndexSearchRes];

            if (dtSearchResults.Rows.Count > 0 && IsRFCAlreadyAdded(dRowSelected))
            {
                dColumn = new DataColumn("RFCID", Type.GetType("System.String"));   
                dtUserRes.Columns.Add(dColumn);

                dColumn = new DataColumn("RFCTitle", Type.GetType("System.String"));
                dtUserRes.Columns.Add(dColumn);

                dColumn = new DataColumn("RFCRevision", Type.GetType("System.String"));
                dtUserRes.Columns.Add(dColumn);

                dColumn = new DataColumn("TriggerAssociation", Type.GetType("System.String"));
                dtUserRes.Columns.Add(dColumn);

                dColumn = new DataColumn("TriggerStatus", Type.GetType("System.String"));
                dtUserRes.Columns.Add(dColumn);

                //setting RFCID as primary key for the datatable UserResults
                dtUserRes.PrimaryKey = new DataColumn[] { dtUserRes.Columns[0] };

                dRow = dtUserRes.NewRow();
                dRow["RFCID"] = dRowSelected["RFCID"];
                dRow["RFCTitle"] = dRowSelected["RFCTitle"];
                dRow["RFCRevision"] = dRowSelected["RFCRevision"];
                dRow["TriggerAssociation"] = "Manual";
                dRow["TriggerStatus"] = "N";
                dtUserRes.Rows.Add(dRow);

                if (UserResults == null)
                {
                    UserResults = new DataTable();
                }
                UserResults.Merge(dtUserRes, true);

            }
        }
        catch (Exception objEx)
        {
            ErrorHandler.ShowErrorMessage(strErrorCaption, "Error occured while Adding RFC from Search results to Execution List", objEx.Message);
        }
        InfoHandler.LogInfo("Exiting AddExecute");
    }

`

Thanks NallsKarthi

1
Could you add some code of your viewmodel, such as : what is the ItemsSource?Damascus

1 Answers

2
votes

It is normal, you are not asking the ComboBox to be populated by many values.

As long as I understand correctly your problem, you have

<ComboBox ItemsSource="{Binding Path=OrderStatus, Mode=TwoWay}" SelectedItem="{Binding Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

What is the type of the object OrderStatus ? And moreover, where is situated OrderStatus?

I'd say as a suggestion:

<ComboBox ItemsSource="{Binding Path=DataContext.OrderStatus,
  RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
  SelectedItem="{Binding Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

(if you are in a Window, change the AncestorType to Window)

To be sure, I'd need more code here, especially: the declaration of your ItemsSource, and the declaration of the OrderStatus