C#WPF从SQL Server 2014填充datagrid组合框列

时间:2016-04-07 20:22:44

标签: c# wpf xaml combobox datagrid

我正在编写一个将填充DataGrid的C#程序。我还希望它填充我有一个组合框的列。我有组合框的选择列表部分正常工作我只是不能用数据库中的数据填充该列。我认为我真的很接近,我只是无法弄清楚我错过了什么。

这是我到目前为止提出的XAML。

<Window x:Class="datagridcombobox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:datagridcombobox"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>

        <DataGrid AlternatingRowBackground="#FFC4B0B0" Height="200" Width="500"  AutoGenerateColumns="False" Name="grdEmployee" Margin="2,0,1,-81">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Terminal" x:Name="comboboxColumn1"  SelectedValueBinding="{Binding Terminal}"/>
                <DataGridTextColumn Binding="{Binding Terminal}" Width="100" Header="Terminal"/>
                <DataGridTextColumn Binding="{Binding Applicator}" Width="100" Header="Applicator"/>
                <DataGridTextColumn Binding="{Binding applicator_Type}" Width="100" Header="Applicator Type"/>
                <DataGridTextColumn Binding="{Binding Hand_tool}" Width="100" Header="Hand Tool"/>
                <DataGridTextColumn Binding="{Binding Hand_Tool_Type}" Width="100" Header="Hand Tool Type"/>
                <DataGridTextColumn Binding="{Binding Wire_Gauge}" Width="100" Header="Wire Gauge"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

C#代码

{
    DataTable Lds;
    SqlDataAdapter adapter;
    SqlConnection connection;

    public List<string> GVComboBox { get; set; }

    public MainWindow()
    {
        InitializeComponent();
    }

    public void Step1()
    {
        Common.sqlApplicator = "Select Terminal,Applicator,applicator_Type,Hand_tool, Hand_Tool_Type ,Wire_Gauge,Crimp_Height as 'Core Crimp Height',Conductor_Crimp_Width as 'Core Crimp Width',Insulator_Crimp_Height as 'Insulation Crimp Height',Insulator_Crimp_Width as 'Insulation Crimp Width',Tooling,Notes,Strip_Length as 'Strip Length',Applicator_Status as 'Applicator Status' ,ID from Tooling_Specs where Applicator like '2151526-1%' order by Applicator";

        try
        {
            connection.Open();
            adapter = new SqlDataAdapter(Common.sqlApplicator, connection);
            Lds = new DataTable("Applicator");
            adapter.Fill(Lds);
            grdEmployee.ItemsSource = Lds.DefaultView;

        }
        catch (Exception E)
        {
            MessageBox.Show(E.ToString());
        }
        finally
        {
            connection.Close();
        };

        GVComboBox = new List<string>() { "CRV Owned", "Customer Owned", "CRV Leased", "Customer Leased", "" };
        comboboxColumn1.ItemsSource = GVComboBox;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        connection = new SqlConnection(Common.ConnectionString);
        Step1();
    }
}

1 个答案:

答案 0 :(得分:0)

SqlCommand cmd = new SqlCommand("SELECT * FROM YourDataBase", yourConnectionName);
SqlDataReader read = null;
read = cmd.ExecuteReader();
while(read.Read())
{
    YourComboBox.Items.Add(read["YourValue"].ToString() + "read["value1"].ToString()+ "StringValue"); 
}
read.Close();