I'm trying to bind my database to a combobox ("ProjectComboBox"), and just can't seem to get it to to work. I've tried passing it through in XAML, as well as backend code, but Combobox is always blank. Any help would be appreciated.
SQL Server (local) database: "Database1"
Data source:(DataSet1); Table:(ProjectTable);
combobox name: "ProjectComboBox"
Here's my XAML code:
<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());
}
}
}
}
comboBox_SelectionChanged, which will never fire because the combo box is empty so it can't be changed to anything. I hope that's a missing}afterInitializeComponent();.