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" > <formataGridView 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
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" > <formataGridView 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