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();
}
}
}
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')what this message is telling you is that it didn't find the propertyValueon the sourceof the binding of a TextBox named "valueTextBox". Looking at the xaml<TextBox x:Name="valueTextBox" Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>no source is defined for the binding so the source will be it's datacontex (or in this case, the datacontex of the first parent with a set datacontext). This is the stackpanelSo the source is
compDataTablewhich is defined in the code behind (you should probably explicitly set the acess modifiers, I was surprised to see it was acessible).In the code behind, I can see
compDataTable = compDataSet.Tables[0];SocompDataTableis of typeTablebut the type Table doesn't have a property namedValueso the binding fails.Regarding the other error, you are treating
compDataTableas a collection and trying to access theNameproperty of one of it's items which of course fail.My guess is that you meant to access the rows of this table so you should check this link which has an exemple of how to acess the data.
*Example in the link