2-Way databinding with Entity Framework and WPF DataGrid , is Possible ?
- by Panindra
i am working on POS application using SQL CE , WPF , Entity framework 3.5sp2 and iam trying to use data grid as my Order Entry Control for User to enter Products Order .
Iam plannning to bind this to enitiy frmae work model , abd looking for 2 way updating ?
private void button1_Click(object sender, RoutedEventArgs e)
{
using (MasterEntities nwEntities = new MasterEntities())
{
var users = from d in nwEntities.Companies
select new { d.CompanyId, d.CompanyName, d.Place };
listBox1.DataContext = users;
dataGrid1.DataContext = users;
// foreach (String c in customers)
// {
// MessageBox.Show(c.ToString());
// }
}
}
When try to double clikc on the datagrid it through s a error with
Caption " Invalid Operation Execption was unhandled " and
Message " A TwoWay or OneWayToSource binding cannot work on the read-only property 'CompanyId' of type '<f__AnonymousType0`3[System.Int32,System.String,System.String]'.
whats wrong here
and my xaml coding goes like this
<Grid>
<ListBox Name="listBox1" ItemsSource="{Binding}" />
<Button Content="Show " Name="button1" Click="button1_Click" />
<DataGrid AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn Header=" ID" Binding="{Binding CompanyId}"/>
<DataGridTextColumn Header="Company Name" Binding="{Binding CompanyName}"/>
<DataGridTextColumn Header="Place" Binding="{Binding Place}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
EDITED :
i made the changes shown by @vorrtex, But, then i added another button to save the chages and in button click event i added follwing code , butit showing Updating error
private void button2_Click(object sender, RoutedEventArgs e)
{
nwEntities.SaveChanges();
}