Problem with loading data from BLL to ComboboxColumn

May 1, 2009 at 7:57 PM
Hi,
I Created a sample application for studying the DataGrid in WPF Toolkit, The First Window was for creating customers by entering name into the datagridtextboz column and collecting the values to a binded observablecollection of Customer class. and it worked fine. the next window contained datagridcombobox column for choosing customer, a templated column for entering date, a textbox column for entering log info. this also worked fine except the loading part. i can choose a customer from the combobox and move to the next cell and the selected customer is displayed on the customer cell (only after going through a truck full of webpages and spending half day). after entering all the entries i am able to update changes to the database too. but the problem is when i bind the ObservableCollection of Type Note (Properties Customer Customer, DateTime Date,String Log) which is loaded with the Note objects from database, the date and log fields are displayed correctly. where as theCustomer Cells are blank. this is the code for window2. pls let me know how to fix this.

<

 

Window x:Class="SampleApplication.Window2"

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

xmlns:bll="clr-namespace:SampleApplicationBLL;assembly=SampleApplicationBLL"

 

 

xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"

 

 

xmlns:local="clr-namespace:SampleApplication"

 

 

Title="Window2" Height="300" Width="500" >

 

 

 

<Window.Resources>

 

 

 

<ObjectDataProvider x:Key="CustomerList" ObjectType="{x:Type local:CustomerList}" MethodName="GetCustomers" />

 

 

 

 

</Window.Resources>

 

 

 

<Grid>

 

 

 

 

<!--The following namespace declaration may be necessary for you to add to the root element of this XAML file.-->

 

 

 

<!--xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"-->

 

 

 

<toolkit:DataGrid AutoGenerateColumns="False" AlternationCount="2" ItemsSource="{Binding }" CanUserAddRows="True" CanUserDeleteRows="True" >

 

 

 

<toolkit:DataGrid.Columns>

 

 

 

<toolkit:DataGridComboBoxColumn Header="Customer" ItemsSource="{Binding Source={StaticResource CustomerList}}" SelectedValuePath ="ID" DisplayMemberPath="Name" SelectedItemBinding ="{Binding Path=Person, Mode=TwoWay}" Width="100" />

 

 

 

 

<toolkit:DataGridTemplateColumn Header="Date" SortMemberPath="Date" Width="100">

 

 

 

<toolkit:DataGridTemplateColumn.CellTemplate>

 

 

 

<DataTemplate>

 

 

 

<TextBlock Text="{Binding Path=Date, StringFormat=\{0:d\}}" />

 

 

 

</DataTemplate>

 

 

 

</toolkit:DataGridTemplateColumn.CellTemplate>

 

 

 

<toolkit:DataGridTemplateColumn.CellEditingTemplate>

 

 

 

<DataTemplate>

 

 

 

<toolkit:DatePicker SelectedDate="{Binding Path=Date, Mode=TwoWay}" SelectedDateFormat="Short" />

 

 

 

</DataTemplate>

 

 

 

</toolkit:DataGridTemplateColumn.CellEditingTemplate>

 

 

 

</toolkit:DataGridTemplateColumn>

 

 

 

<toolkit:DataGridTextColumn Binding="{Binding Log}" Header="Log" Width="*"/>

 

 

 

</toolkit:DataGrid.Columns>

 

 

 

</toolkit:DataGrid>

 

 

 

 

</Grid>

 

</

 

Window>

 


Code For Note.cs is this

 



 

using

 

System;

 

 

 

 

using

 

System.Collections.Generic;

 

 

 

 

using

 

System.Linq;

 

 

 

 

using

 

System.Text;

 

 

 

 

using

 

System.ComponentModel;

 

 

 

 

using

 

System.Collections.ObjectModel;

 

 

 

 

using

 

SampleAppicationDAL.NotesDataSetTableAdapters;

 

 

 

 

using

 

SampleAppicationDAL.CustomerDataSetTableAdapters;

 

 

 

 

using

 

SampleAppicationDAL;

 

 

 

 

namespace

 

SampleApplicationBLL

 

{

 

public class Note : IEditableObject ,INotifyPropertyChanged

 

{

 

public Note()

 

{

}

 

private int id=-1;

 

 

public int ID

 

{

 

get

 

 

 

 

 

{

 

return id;

 

}

 

set

 

 

 

 

 

{

id =

value;

 

RaisePropertyChanged(

"ID");

 

}

}

 

private Customer customer;

 

 

public Customer Person

 

{

 

get

 

 

 

 

 

{

 

return customer;

 

}

 

set

 

 

 

 

 

{

customer =

value;

 

RaisePropertyChanged(

"Customer");

 

}

}

 

private DateTime date=DateTime.Today ;

 

 

public DateTime Date

 

{

 

get

 

 

 

 

 

{

 

return date;

 

}

 

set

 

 

 

 

 

{

date =

value;

 

RaisePropertyChanged(

"Date");

 

}

}

 

private string note;

 

 

public string Log

 

{

 

get

 

 

 

 

 

{

 

return note;

 

}

 

set

 

 

 

 

 

{

note =

value;

 

RaisePropertyChanged(

"Log");

 

}

}

 

public bool Create()

 

{

 

bool saved = false;

 

 

if (customer.ID >0 )

 

{

 

NotesTableAdapter ta = new NotesTableAdapter();

 

 

NotesDataSet.NotesDataTable tb = new NotesDataSet.NotesDataTable();

 

 

NotesDataSet.NotesRow tr = tb.NewNotesRow();

 

tr.NoteID = (

int)ta.NewID();

 

tr.CustID = customer.ID ;

tr.LDate = date;

tr.Note = note;

tb.AddNotesRow(tr);

saved= ta.Update(tb) > 0;

 

if (saved)

 

id = tr.NoteID;

}

 

return saved;

 

}

 

public bool Update()

 

{

 

bool saved = false;

 

 

if (id > 0)

 

{

 

NotesTableAdapter ta = new NotesTableAdapter();

 

 

NotesDataSet.NotesDataTable tb = ta.GetDataByID(id);

 

 

NotesDataSet.NotesRow tr =(NotesDataSet.NotesRow) tb.Rows[0];

 

 

//tr.NoteID = (int)ta.NewID();

 

 

 

 

 

tr.CustID = customer.ID ;

tr.LDate = date;

tr.Note = note;

 

//tb.AddNotesRow(tr);

 

 

 

 

 

saved = ta.Update(tb) > 0;

 

}

 

return saved;

 

}

 

public bool Delete()

 

{

 

bool deletd = false;

 

 

if (id > 0)

 

{

 

NotesTableAdapter ta = new NotesTableAdapter();

 

 

NotesDataSet.NotesDataTable tb = ta.GetDataByID(id);

 

 

NotesDataSet.NotesRow tr = (NotesDataSet.NotesRow)tb.Rows[0];

 

 

//tr.NoteID = (int)ta.NewID();

 

 

 

 

 

tr.Delete();

 

//tb.AddNotesRow(tr);

 

 

 

 

 

deletd = ta.Update(tb) > 0;

 

}

 

return deletd ;

 

}

 

 

public static ObservableCollection<Note> getNotes()

 

{

 

ObservableCollection<Note> notes = new ObservableCollection<Note>();

 

 

NotesTableAdapter ta = new NotesTableAdapter();

 

 

NotesDataSet.NotesDataTable tb = ta.GetData();

 

 

CustomerTableAdapter cta = new CustomerTableAdapter();

 

 

CustomerDataSet.CustomerDataTable ctb = new CustomerDataSet.CustomerDataTable();

 

 

 

foreach (NotesDataSet.NotesRow row in tb.Rows)

 

{

 

Note note = new Note();

 

note.id = row.NoteID;

ctb=cta.GetDataByID(row.CustID);

 

Customer customer = new Customer();

 

 

CustomerDataSet.CustomerRow ctr = ctb[0];

 

customer.ID=ctr.CustID;

customer.Name=ctr.CName;

note.Person =customer ;

note.date = row.LDate;

note.note = row.Note;

notes.Add(note);

}

 

return notes;

 

}

 

 

 

 

 

#region

 

events

 

 

public event NoteEndEditEventHandler ItemEndEdit;

 

 

 

 

#endregion

#region

 

IEditableObject Members

 

 

public delegate void NoteEndEditEventHandler(IEditableObject sender);

 

 

public void BeginEdit()

 

{

}

 

public void CancelEdit()

 

{

}

 

public void EndEdit()

 

{

 

if (ItemEndEdit != null)

 

{

ItemEndEdit(

this);

 

}

}

 

 

 

#endregion

#region

 

INotifyPropertyChanged Members

 

 

public event PropertyChangedEventHandler PropertyChanged;

 

 

private void RaisePropertyChanged(string property)

 

{

 

if (PropertyChanged != null)

 

{

PropertyChanged(

this, new PropertyChangedEventArgs(property));

 

}

}

 

 

 

#endregion

 

 

 

 

}

}

 

This is the error i am getting on output window

 

 

System.Windows.Data Error: 39 : BindingExpression path error: 'Person' property not found on 'object' ''Object' (HashCode=31227247)'. BindingExpression:Path=Person; DataItem='Object' (HashCode=31227247); target element is 'TextBlockComboBox' (Name=''); target property is 'SelectedItem' (type 'Object')

System.Windows.Data Error: 39 : BindingExpression path error: 'Date' property not found on 'object' ''Object' (HashCode=31227247)'. BindingExpression:Path=Date; DataItem='Object' (HashCode=31227247); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Error: 39 : BindingExpression path error: 'Log' property not found on 'object' ''Object' (HashCode=31227247)'. BindingExpression:Path=Log; DataItem='Object' (HashCode=31227247); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

 

pls let me know what to do

May 1, 2009 at 8:19 PM
i found out that the error displaying (error 39) is not generated from my code. if i change the canuseraddrow property to false this error is not showing. but my problem is not fixed, i am still not able to load the custyomer object to cell.