Hi, I am developing a WPF browser application using Linq and your datagrid but I am having major problems trying to delete a record. From looking through various forums and your help I have got to the point where I can retrieve the data usning LINQ and settign the result as the DataSource. I can add a record fine but whenit comes to deleteing I just cant seem to tie it togther. A number of forums have suggesed that once you know what records you need to delete you query it from teh database using LINQ then plase the result into a DeleteOnSubmit command. So I have the following code:
private void datagridMapping_RecordsDeleting(object sender, Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs e)
{
DataCentreDataContext dsMapping = new DataCentreDataContext();
foreach (var rec in datagridMapping.SelectedItems.Records)
try
var mappingres =
from mappingItem in dsMapping.DATAMAPPINGs
where mappingItem.INDEX == rec.???????????????
select mappingItem;
DATAMAPPING delRecord = new DATAMAPPING();
delRecord = (DATAMAPPING)mappingres;
dsMapping.DATAMAPPINGs.DeleteOnSubmit(delRecord);
}
catch (Exception ex)
MessageBox.Show("Error", "Error - " + ex.Message);
dsMapping.SubmitChanges();
I have placed ?????? where I am having the trouble. I guess I may have this very wrong so any help would be very much appreciated.
Regards,
Kevin.
Hello Kevin,
When having a quick look at your code, this is what I notice:
1. The records deleting event arguements expose a collection of the records for deletion as well (e.Records), which is a much more flexible than the SelectedItems.Records collection.
2. Your LINQ query will return an IEnumerable object, so I think you will not be able to cast mappingres to DATAMAPPING, as I believe one is collection and the other not.
3. I think you do not need this LINQ query at all. As you are adding the underlying data business object to the DeleteOnSubmit method, you can do this directly from the DataRecord. Each DataRecord exposes a Dataitem property, which will be your underlying object, in your case DataMapping so you can pass that in the DeleteOnSubmit method directly. Note, that you have to cast the Record to DataRecord, when iterating though the e.Record collection.
Let me know if you have questions on this.
Hi,
I am struggling with the whole concept to be honest. I am new to LINQ and WPF.
I thought the insert was working the code was placed in the RecordUpdated event and worked, but I now get an error when I update an existsing record.
I cant seem to get my gead around how to simply link up a table and the grid to enable simple Add/Edit/Remove functionality.
I dont want to expose all the columns of the tabel to the grid.
Do you have a sample project I may be able to get some idea of what I am dealing with.
Cheers, as awlays.