DataGridComboBoxColumn从DataView设置值

时间:2014-08-15 17:46:47

标签: c# wpf datagrid datagridcomboboxcolumn

我有以下DataGrid:

<DataGrid Name="RateTable"
              ItemsSource="{Binding RateTableDataView}"
              Margin="20"
              AutoGenerateColumns="False"
              Width="725">

        <DataGrid.Columns>
            <DataGridTextColumn Header="Rate Table" 
                                Width="75" 
                                Binding="{Binding RateTable}" 
                                ElementStyle="{StaticResource CellStyle}" 
                                IsReadOnly="True"/>
            <DataGridComboBoxColumn Header="Match Type" 
                                    Width="100" 
                                    ItemsSource="{Binding Source={StaticResource MatchTypeCollection}}"
                                    SelectedValueBinding="{Binding RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type UserControl}}, Path=DataContext.SelectedMatchType}" 
                                    SelectedItemBinding="{Binding MatchType}"/>
            <DataGridComboBoxColumn Header="EARNDED" 
                                    Width="100" 
                                    ItemsSource="{Binding Source={StaticResource EARNDEDCollection}}"
                                    SelectedItemBinding="{Binding EARNDED}" />
        </DataGrid.Columns>

    </DataGrid>

这可以按预期工作除了组合框的值没有设置为它们在ItemSource中的值。

例如,我从SQL返回并放入RateTableDataView的数据已将EARNDED设置为“D2000”,但是当显示该表时,组合框中的值为空。

如何使用RateTableDataView的值初始化组合框?

修改

以下是EARNDED的代码

在ViewModel中,我创建了一个EARNDED值的ObservableCollection:

    public ObservableCollection<String> EARNDEDComboBoxValues
    {
        get
        {
            DataSet _dataset = new DataSet("TempDataSet");

            // Get reference to DelphiaConfigInfo
            DelphiaConfigModel _config =
                (App.Current.Resources["Locator"] as ViewModelLocator).SystemSetupViewModel.DelphiaConfigInfo;

            using (SQLClass _sql = new SQLClass(_config.DatabaseInfo))
            {
                using (SqlConnection _conn = _sql.Connection)
                {
                    SqlCommand _cmd = _conn.CreateCommand();
                    _cmd.CommandType = CommandType.Text;
                    _cmd.CommandText = "select EARNDED from [dbo].[UPDTLM]";

                    SqlDataAdapter _adapter = new SqlDataAdapter(_cmd);

                    _adapter.Fill(_dataset);
                }
            }

            // Get List of 'MatchType' values using Linq
            List<String> _values =
                _dataset.Tables[0].AsEnumerable()
                .Select(datarow => datarow.Field<String>("EARNDED")).ToList<String>();

            // Convert List to ObservableCollection 
            return new ObservableCollection<String>(_values);
        }
    }

在XAML中,我将静态资源调到EARNDEDComboBoxValues(显然DataGRidComboBoxColumn无法绑定到非静态属性):

        <CollectionViewSource x:Key="EARNDEDCollection"
                              Source="{Binding RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type UserControl}}, Path=DataContext.EARNDEDComboBoxValues}" />

组合框正在填充数据,当我打开组合框时会出现这些数据,但是从SQL中检索到的初始值未在组合框中设置。组合框为空(未选择任何内容)。

0 个答案:

没有答案