Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c wpf绑定组合框到SQL Server数据库_C#_Sql Server_Wpf_Combobox_Dataset - Fatal编程技术网

C# c wpf绑定组合框到SQL Server数据库

C# c wpf绑定组合框到SQL Server数据库,c#,sql-server,wpf,combobox,dataset,C#,Sql Server,Wpf,Combobox,Dataset,我试图将我的数据库绑定到combobox项目combobox,但似乎无法使其正常工作。我试着在XAML中传递它,以及后端代码,但Combobox总是空的。任何帮助都将不胜感激 SQL Server本地数据库:数据库1 数据来源:数据集1;表:项目表 组合框名称:ProjectComboBox 以下是我的XAML代码: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/wi

我试图将我的数据库绑定到combobox项目combobox,但似乎无法使其正常工作。我试着在XAML中传递它,以及后端代码,但Combobox总是空的。任何帮助都将不胜感激

SQL Server本地数据库:数据库1

数据来源:数据集1;表:项目表

组合框名称:ProjectComboBox

以下是我的XAML代码:

<Window x:Class="WpfApplication1.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:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="500" Width="850">
<Grid>
    <ComboBox x:Name="ProjectComboBox"
              ItemsSource="{Binding Path=ProjectTable}"
              DisplayMemberPath="ProjectName" 
              SelectedValuePath="RFIDirectory" 
              HorizontalAlignment="Left" 
              VerticalAlignment="Top" 
              Width="297" Height="26" 
              SelectionChanged="comboBox_SelectionChanged">
    </ComboBox>
}

不能直接绑定到SQL Server表记录。但是,可以绑定到表示表记录的对象列表。 正如@Paul Abbott提到的,一旦表单加载,您必须首先初始化组合框项。或者一种丑陋的方法是添加一个虚拟记录,一旦选择它,将引发selection changed事件,然后执行代码。 不要为您的连接打开和sql数据适配器执行单独的try-catch块。这是多余的

在sql连接上使用using语句可在连接后正确关闭和处置

public MainWindow()
{
    InitializeComponent();
    UpdateItems(); // Maybe initialize here?
}

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ProjectComboBox.Items.Clear(); // Remove this. AFAIK, the selected item will be cleared.
    UpdateItems();
}

private void UpdateItems()
{
    try
    {
        using(SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Database1;Integrated Security=True"))
        {
            con.Open();
            SqlDataAdapter ProjectTableTableAdapter = new SqlDataAdapter("SELECT * FROM PROJECTNAME", con);
            DataSet1 ds = new DataSet1();
            ProjectTableTableAdapter.Fill(ds, "t");

            ProjectComboBox.ItemsSource = ds.Tables["t"].DefaultView;
            ProjectComboBox.DisplayMemberPath = "ProjectName";
            ProjectComboBox.SelectedValuePath = "RFIDirectory";
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    } 
}

<ComboBox x:Name="ProjectComboBox"
      ItemsSource="{Binding Path=ProjectTable}" // Not sure of what the impact will be by adding this because you have already defined the item source on your code-behind. I'd prefer you remove this and use XAML binding when you're following a design pattern like MVVM or MVPVM.
      DisplayMemberPath="ProjectName" 
      SelectedValuePath="RFIDirectory" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" 
      Width="297" Height="26" 
      SelectionChanged="comboBox_SelectionChanged">
</ComboBox>

您正在comboBox_SelectionChanged中填充组合框,该组合框将永远不会触发,因为组合框为空,因此无法更改为任何内容。我希望在初始化Component;之后缺少一个};。
public MainWindow()
{
    InitializeComponent();
    UpdateItems(); // Maybe initialize here?
}

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ProjectComboBox.Items.Clear(); // Remove this. AFAIK, the selected item will be cleared.
    UpdateItems();
}

private void UpdateItems()
{
    try
    {
        using(SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Database1;Integrated Security=True"))
        {
            con.Open();
            SqlDataAdapter ProjectTableTableAdapter = new SqlDataAdapter("SELECT * FROM PROJECTNAME", con);
            DataSet1 ds = new DataSet1();
            ProjectTableTableAdapter.Fill(ds, "t");

            ProjectComboBox.ItemsSource = ds.Tables["t"].DefaultView;
            ProjectComboBox.DisplayMemberPath = "ProjectName";
            ProjectComboBox.SelectedValuePath = "RFIDirectory";
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    } 
}

<ComboBox x:Name="ProjectComboBox"
      ItemsSource="{Binding Path=ProjectTable}" // Not sure of what the impact will be by adding this because you have already defined the item source on your code-behind. I'd prefer you remove this and use XAML binding when you're following a design pattern like MVVM or MVPVM.
      DisplayMemberPath="ProjectName" 
      SelectedValuePath="RFIDirectory" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" 
      Width="297" Height="26" 
      SelectionChanged="comboBox_SelectionChanged">
</ComboBox>