[英]c# wpf bind combobox to SQL Server database
I'm trying to bind my database to a combobox ("ProjectComboBox"), and just can't seem to get it to to work. 我正在尝试将我的数据库绑定到一个组合框(“ProjectComboBox”),似乎无法让它工作。 I've tried passing it through in XAML, as well as backend code, but Combobox is always blank.
我试过在XAML中传递它,以及后端代码,但Combobox总是空白的。 Any help would be appreciated.
任何帮助,将不胜感激。
SQL Server (local) database: "Database1" SQL Server(本地)数据库:“Database1”
Data source:(DataSet1); 数据源:(DataSet1); Table:(ProjectTable);
表:(ProjectTable);
combobox name: "ProjectComboBox" 组合框名称:“ProjectComboBox”
Here's my XAML code: 这是我的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>
And here's my backend code: 这是我的后端代码:
namespace WpfApplication1
public partial class MainWindow : Window
{
public DataSet1 ProjectTable { get; set; }
public MainWindow()
{
InitializeComponent();
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ProjectComboBox.Items.Clear();
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Database1;Integrated Security=True");
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
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());
}
}
}
} }
Don't do separate try-catch block for your connection open and sql data adapter. 不要为连接打开和sql数据适配器单独的try-catch块。 It is redundant.
这是多余的。
Use using statement on your sql connection to properly close and dispose after. 在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.