add/edit/del data in datagrid binding OleDb?

Nov 14, 2008 at 4:39 PM

first, thanks the datagrid released

i wonder that whether or not datagrid can do add/edit/del data in datagrid which is binded with access DB ? now i only have it bind with my access db to present my data. but how do the add/edit/del? however here is my bind code

  dtAllBusiness.Rows.Clear(); //  a datatable
 accessCon = new OleDbConnection();
 accessCon.ConnectionString = Common.GetConnectionString();
 accessCon.Open();

 accessCmd = new OleDbCommand(@"SELECT * FROM DailyOperation", accessCon);
 accessCmd.CommandType = CommandType.Text;
 accessCmd.ExecuteNonQuery();
 OleDbDataAdapter accessDaPro = new OleDbDataAdapter(accessCmd);
 accessDaPro.Fill(dtAllBusiness);

 Binding bind = new Binding();
 bind.Source = dtAllBusiness;
 dataGVGeneal.SetBinding(DataGrid.ItemsSourceProperty, bind);

 

and my wpf datagrid is looks like this

 <dg:DataGrid x:Name="dataGVGeneal" EnableColumnVirtualization="True" EnableRowVirtualization="True"
 AutoGenerateColumns="False" ColumnHeaderStyle="{StaticResource dgHeaderStyle}"
 RowStyle="{StaticResource dgRowStyle}" CellStyle="{StaticResource dgCellStyle}" >

 <dg:DataGrid.Columns> 
 <dg:DataGridTextColumn Header="Operation_id" Width="SizeToCells" Binding="{Binding Path=Operation_id}"/>
 <dg:DataGridTextColumn Header="Category_id" Width="SizeToCells" Binding="{Binding Path=Category_id}"/>
 <dg:DataGridTextColumn Header="Product_name" Width="SizeToCells" Binding="{Binding Path=Product_name}"/>
 <dg:DataGridTextColumn Header="Price" Width="SizeToCells" Binding="{Binding Path=Price}"/>
 <dg:DataGridTextColumn Header="Qua" Width="SizeToCells" Binding="{Binding Path=Qua}" />
 <dg:DataGridTextColumn Header="User_name" Width="SizeToCells" Binding="{Binding Path=User_name}"/>
 <dg:DataGridTextColumn Header="Customer_number" Width="SizeToCells" Binding="{Binding Path=Customer_number}"/>
 <dg:DataGridTextColumn Header="OrderType" Width="SizeToCells" Binding="{Binding Path=OrderType}"/>
 <dg:DataGridTextColumn Header="Driver" Width="SizeToCells" Binding="{Binding Path=Driver}" />

 <dg:DataGridTemplateColumn Header="Date" MinWidth="100">
 <dg:DataGridTemplateColumn.CellEditingTemplate>
 <DataTemplate>
 <dg:DatePicker x:Name="DatePickerReport" SelectedDate="{Binding Date}" SelectedDateFormat="Short" CalendarClosed="DatePickerReport_CalendarClosed"/>
 </DataTemplate>
 </dg:DataGridTemplateColumn.CellEditingTemplate>
 <dg:DataGridTemplateColumn.CellTemplate>
 <DataTemplate>
 <TextBlock Text="{Binding Date, StringFormat=d}"/>
 </DataTemplate>
 </dg:DataGridTemplateColumn.CellTemplate>
 </dg:DataGridTemplateColumn>

 </dg:DataGrid.Columns>
 </dg:DataGrid>

Coordinator
Nov 17, 2008 at 12:46 PM
When you bind to the DataGrid, is the CollectionView a BindingListCollectionView?  If so, you should get that all for free.
Nov 17, 2008 at 2:18 PM

thank you for your valuble reply

but i just use the same way to bind as what i used to bind gridview in listview...

can you please explain more better about this CollectionView a BindingListCollectionView? all i have done with my datagrid is what i post you seen,,

Coordinator
Nov 17, 2008 at 3:28 PM
First of all, is add/edit/delete not working?

To understand which is your default collectionview you can do something like this:

ICollectionView view = CollectionViewSource.GetDefaultView(dataGVGeneral.ItemsSource);
BindingListCollectionView blvc = view as BindingListCollectionView;
Nov 17, 2008 at 6:38 PM

thank you very much for the more explaination

my original question is how make add/edit/delete working with the access db ? i didnt even know how make them work, you might read my question on the top, i do can bind the data i retrieved from access DB onto Datagrid,,, but if i do add/edit/delete , how would i do tehm? i mean if it is asp.net, there is some kind of command that allow you to do add/edit/delete functions. But in the datagrid control how do i impletement add/edit/delete working? what is these codes???

i do know that if i want to add/edit/delete data from my access DB, i have to write some access DB command like INSERT, UPDATE, DELETE from it.

For example, in asp.net gridview, if i click edit command on one row, and then that row will become edit template which are some editble controls (textbox,dropdownlist) let me to edit data, and again after i finally finish editting, i just simply click update, it will save into DB,,,but seems in the datagrid, it doesnt have this sort of functions?

thank you very much again

Nov 17, 2008 at 8:49 PM
Hi,

I recently published an article which contains examples of how to handle add/edit/delet operations and update the database accordingly:

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

The examples use typed datasets which would be a suitabel mechanism for interfacing with an access Db.

Hope that helps,
Colin E.


Nov 18, 2008 at 9:33 AM

hi

thank you very much for your help colin, i actually have seen your codeproject before i post this questions, i found the add/edit/del functions which you implement them is on the principle that use event to trigger  add/edit/del commands, for example like update date column, click date to become editTemplate, and select which date you want, and then close it, so there is some kind close event to trigger update database command behind, am i right? (just quick looked on your code, )

so i wonder this datagrid does not have any sort of methods like asp.net gridview, use update,insert,delete commands attach with it? rather than do them independently ?

however i am studying your codes now, they are defo help me alot, again thank you very much for your contribution, colin!

hope i can understand your codes :) T_T. i am coding virgin;

tao

Nov 18, 2008 at 9:42 AM
Hi,

Your understanding is almost correct. The basic principle is that you provide your data provider to the DataGrid. The Datagrid takes care of add/edit/removing items from your bound data. Your data provider provides events which you can handle when the data changes. You use these events to synchronise your database. In this way your sycnhronisation code is not coupled too the fact that your data is presented by a DataGrid - you can for example bind it to ListView or some TextBoxes and still use the same update mechanism.

I'm afraid the WPD DataGrid is quite different to the ASP one. It's a shame - I really lik teh ObjectDataProvider concept in ASP.NET. It is a closer relative to the WinForms DataGridView (but not that close ;-) ).

All the best,
Colin E.
Nov 18, 2008 at 11:13 AM
Edited Nov 18, 2008 at 11:14 AM

Colin

Now i have it, really i do understand now, i ll put efforts on your code, hope get my problem solved ,,, but i have to say it again, allow me to represent all us c# virgin to indeed appreciate your valuble free codeproject there, it is defo helping a lot people, thank you again.

by the way, do you even found the WPF is quite slow ? my pc is very good performance now, but when i bind my access data to datagrid which is quite slow to response, (about 200 records, in future could be more than 100000 records). how would i avoid this situation while my DB getting huge?

also i found if i bind some (less than 100 records) into listview (gridview in listview.view) by using button to present them in wrappannel , it is crazy slow, is not synchronizing with my cursor. anyway my code is below 

  <ListView ItemsSource="{Binding}" x:Name="lvProductList1" 
Background="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
BorderBrush="{x:Null}" SelectionMode="Single"
>
<ListView.View>
<GridView x:Name="gvProByCategory">
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Hidden" />
<Setter Property="Height" Value="0" />
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>  
<Button Foreground="White" Style="{DynamicResource GlassButton}" 
VirtualizingStackPanel.IsVirtualizing="True" Tag="{Binding Path=Product_id}"
FontSize="15" Height="70" Width="115" Click="btSelectProduct_Click" Content="{Binding Path=Product_name}">
</Button>  
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>  
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />  // when i change to wrap my items in the listview, its just way to slow,,,if i dont ,its ok( but still slow than winform) i hear that people say should do something like virtual panel, but you have to write yourself, MS doesnt support virtual panel in warp ..is that rit???
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Style>
</ListView>

thank you colin

tao

Nov 18, 2008 at 1:47 PM
Hi,

Sorry - but i have not tried binding large quantities of data. I know that WPF has the concept of UI vitualization which can enhance performance - but it is something I have not looked into.

Regards,
Colin E.
Nov 18, 2008 at 2:33 PM

Colin

thats alrit, dont worry about that,, thank you anyway, iam lookin at your codes now...hope find some way to solve my problme

tao