Wpf/net 3.5 /VS2008 - Binding to Ado.net dataTable problem

  • Thread starter Thread starter Rolf Welskes
  • Start date Start date
R

Rolf Welskes

Hello,
because there are no managed news group for window presentation foundation
and one
has told me I can use this, here my problem:

Here a litte code with a sample program - simple program which binds to an
ado.net table:

<Window x:Class="Test03.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:form="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Title="Window1" Height="414" Width="519"> <Grid Name="grid" Background="LightGray" Height="355" Width="471"> <Grid.Resources> <ObjectDataProvider x:Key="dp" /> </Grid.Resources> <ListBox Margin="12,17,0,0" Name="lbx01" ItemsSource="{BindingSource={StaticResource dp}}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Row[0]"HorizontalAlignment="Left" Width="143" Height="78" VerticalAlignment="Top" /> <TextBox Height="23" Margin="183,17,168,0" Name="tx01"VerticalAlignment="Top" Text="{Binding Source={StaticResource dp}, Path=Row[0],UpdateSourceTrigger=PropertyChanged}"/> <TextBox Height="23" Margin="183,48,168,0" Name="tx02"VerticalAlignment="Top" Text="{Binding Source={StaticResource dp}, Path=Row[1],UpdateSourceTrigger=PropertyChanged}"/> <Button Height="23" Margin="183,80,168,0" Name="btnAdd"VerticalAlignment="Top" Click="btnAdd_Click">Add</Button> <my:WindowsFormsHost Margin="12,152,98,82" Name="windowsFormsHost1" > <form:DataGridView x:Name="dataGridView"/> </my:WindowsFormsHost> </Grid></Window>using System;using System.Data;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Forms;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace Test03{ /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { DataSet ds; CollectionViewSource cvs; public Window1() { InitializeComponent(); ds = new DataSet(); DataTable table = new DataTable(); DataColumn dcName = new DataColumn("Name"); DataColumn dcFirstName = new DataColumn("FirstName"); table.Columns.Add(dcName); table.Columns.Add(dcFirstName); ds.Tables.Add(table); DataRow row = table.NewRow(); row[0] = "Smith"; row[1] = "John"; table.Rows.Add(row); row = table.NewRow(); row[0] = "Sinclaire"; row[1] = "Jill"; table.Rows.Add(row); DataView dv = new DataView(); dv.Table = table; cvs = new CollectionViewSource(); cvs.Source = dv; ObjectDataProvider dp =(ObjectDataProvider)grid.FindResource("dp"); dp.ObjectInstance = cvs.View; dataGridView.DataSource = dv; } private void btnAdd_Click(object sender, RoutedEventArgs e) { BindingListCollectionView bcv =(BindingListCollectionView)cvs.View; DataView dvx = (DataView)bcv.SourceCollection; DataRowView drv = dvx.AddNew(); drv[0] = "new name"; drv[1] = "new firstName"; } }}Run the program and at the bottom in the DataGridView add a new line forexampleas Name aaa as FirstName bbb now change to the line over the new line to addthe record.You get an FatalExceptionEngineError.Now restart the program.Click the Add Button, a record is added. Now click the Add Button again.Now you get again a FatalExceptionEngineError.So you can not add records in DataGridView (WindowsForms) and you cannot addrecordsdirectly with a button (WPF).Thank you for any help.Rolf Welskes
 
Hello,
as I now see, the code text look awful.
If it is not usable I would send it again.

Thank you.
Rolf Welskes
 
Hi Rolf,

I've reproduced the issue using following simplified code:

DataTable dt = new DataTable();
dt.Columns.Add("foo");
dt.Rows.Add("bar");
DataView dv = new DataView(dt);
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = dv;
dv.AddNew();
dv.AddNew();

This is probably related to how DataView internally organize the data:
until you call DataRowView.EndEdit(), the data is not added to the
underlying DataTable. If you try to add the data to the underlying
DataTable directly, then it will be fine. Anyway, I will forward this issue
to WPF team to see if they could give more information.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello,
thank you,
but the problem is,
if you use the DataGridView of windows forms (interop with wpf), and enter a
new record
it make also an dv.AddNew().
But here it is not possible to make a work a round because it's the
datagridview-libary-code which makes
the steps.
Thank you
Rolf Welskes
 
Hi Rolf,

Thanks for your update.

I'm reported this issue to product group. I'll keep you posted.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Rolf,

We have tracked down the issue, it's related to ADO.NET DataView will raise
ListChanged(ItemAdded) event twice (one with AddNew, one with EndEdit --
implicitly called by AddNew internally).

Thanks for your great feedback and sorry for the inconvenience caused.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top