The data from the DataTable doesn't fill the ComboBox or TextBox. Data from a SQL Server database is used to fill a DataTable, which is working fine, and I think the CollectionViewSource is okay as well, but the binding to the display elements isn't showing.
I originally created the view from the TableAdapter designer, and it all worked fine, the data was shown and I could move through the table rows. I needed to code for an SQL connection to the Data Source, so I created a SQL Data Adapter, using it to fill a DataSet then creating a DataTable from that dataset. Stepping thru the debugger, I can see that the DataTable ("compDataTable") contains all the data. I then used this DataTable as the CollectionViewSource ('tbl_CompsViewSource') to bind to my View controls, same as the original TableAdapter did, but when I run it, the View controls are blank. I tried to debug the binding through the System.Diagnostics, but it doesn't show any error except when it tries to fill in the View controls. The Error thrown is:
System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''Char' (HashCode=4325442)'. BindingExpression:Path=Name; DataItem='Char' (HashCode=4325442); target element is 'ComboBox' (Name='nameComboBox'); target property is 'NoTarget' (type 'Object')
and
System.Windows.Data Error: 40 : BindingExpression path error: 'Value' property not found on 'object' ''EnumerableCollectionView' (HashCode=28278595)'. BindingExpression:Path=Value; DataItem='EnumerableCollectionView' (HashCode=28278595); target element is 'TextBox' (Name='valueTextBox'); target property is 'Text' (type 'String')
The XML code is :
<Window x:Class="CompsTabAdapt.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:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="MainWindow" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="tbl_CompsViewSource" Source="{Binding Source=compDataTable}" diag:PresentationTraceSources.TraceLevel="High"/>
</Window.Resources>
<StackPanel DataContext="{Binding tbl_CompsViewSource}">
<Label Content="Compound :"/>
<ComboBox x:Name="nameComboBox" DisplayMemberPath="Name" ItemsSource="{Binding}" IsEditable="True">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<StackPanel Orientation="Horizontal">
<Label Width="120">Value: </Label>
<TextBox x:Name="valueTextBox" Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
</StackPanel>
</StackPanel>
</Window>
and the code behind is :
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace CompsTabAdapt
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SqlDataAdapter CompsDataAdapt = new SqlDataAdapter();
DataSet compDataSet = new DataSet();
DataTable compDataTable = new DataTable();
CollectionViewSource tbl_CompsViewSource = new CollectionViewSource();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
string dbcon = ConfigurationManager.ConnectionStrings["ConnOrigString"].ConnectionString;
using (SqlConnection Connection = new SqlConnection(dbcon))
{
Connection.Open();
CompsDataAdapt = new SqlDataAdapter("SELECT * FROM tbl_Comps", Connection);
CompsDataAdapt.Fill(compDataSet);
Connection.Close();
compDataTable = compDataSet.Tables[0];
}
}
catch
{
dbConnect();
}
finally
{
tbl_CompsViewSource = (CollectionViewSource)(this.FindResource("tbl_CompsViewSource"));
tbl_CompsViewSource.View.MoveCurrentToFirst();
}
}
}