从数据库表中填充DataGrid中的组合框

时间:2016-06-18 09:50:36

标签: c# wpf combobox datagrid

我将WPF DataGrid绑定到SQL服务器表,其中一列是comboBox。我想从另一个表填充comboBox。例如:有两个表 - 工作和员工:

  • 员工拥有EmployeeID,EmployeeName,EmployeeJob_id
  • jobs有JobID,JobName

这是XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="241" HorizontalAlignment="Left" Margin="70,20,0,0" Name="EmployeeDataGrid" VerticalAlignment="Top" Width="365">
            <DataGrid.Columns>
                <DataGridTextColumn Header="EmployeeID" />
                <DataGridTextColumn Header="E_Name" />
                <DataGridTemplateColumn Header="EmployeeJobID">
                     <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox 
                                 ItemsSource="{Binding jobs_DataView, 
                                 RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                DisplayMemberPath="JobName" SelectedValuePath="JobID"
                                SelectedValue="EmployeeJobID"/>
                        </DataTemplate> 
                            
                               
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
代码背后:

 EmployeeDataGrid.DataContext = EmployeeDataView;

我还有一个DataView作为名为jobsDatView的作业表,我想将其用作项目但没有项目添加到组合框中。

0 个答案:

没有答案