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.
Kevin,
Well, for filtering, you can use something like this in the SelectedItemChanged event handler for the tree view:
IQueryable<Product> products = null;
TreeViewItem newItem = e.NewValue as TreeViewItem;
//get the value of the "Filter"
object value = newItem.Header.ToString();
// check for the parent
bool hasParent = newItem.Parent != null ? true : false;
if (hasParent)
// get the property/field to createa the filter for
string property = (newItem.Parent as TreeViewItem).Header.ToString();
switch (property)
case "SupplierID":
products = from product in context.Products
where product.SupplierID.ToString() == value.ToString()
select product;
break;
case "CategoryID":
where product.CategoryID.ToString() == value.ToString()
// rebind
xamDataGrid1.DataSource = products;
Regarding RecordFiltering, this feature is not included in the Express version - only in full and trial - this is why you are getting this exception.
@ ComboBoxes, as the Express versions does not have the XamComboEditor control, you have to retemplate the CellValuePresenter and put a ComboBox inside its template. Then you can assign this style at the CellValuePresenterStyle property of the FieldSettings of a specific field.
Hope this helps.
Out of interest can you apply comboboxes to grid cells in the express version?
Just figured out I am using the express version of the grid so therefore I cant apply Record Filters.
I cant afford the full version at the moment so any ideas of how I can achieve this would be great.
Cheers,
I have applied your method and got the grid to populate however one of the reasons I used Linq in the first place was I have a tree view on the right of the grid which contains a set of values. the idea being the user click on a value and the grid is populated with relevent data. I originally used Linq to query the database using the tree view selected value as the where clause.
I guessed the way round this was to programmatically apply a filter instead. I looked at you documentation and tried what it suggested but I get an error when adding the filter:
private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
FieldLayout fieldLayout = datagridMapping.FieldLayouts[0];
// Create a new RecordFilter for 'Country' field.
RecordFilter filter = new RecordFilter();
filter.FieldName = "MEASUREMENT";
filter.Conditions.Add(new ComparisonCondition(ComparisonOperator.Equals, "SYSBP"));
// Add the RecordFilter to field layout's RecordFilters collection.
fieldLayout.RecordFilters.Add(filter);
The last line throws an error saying FieldLayout does not contain a definition for RecordFilters.
Any ideas?
Cheers as always,
Kevin
I have created a small sample with a XamDataGrid bound to a database through LINQ queries. You do not need to handle all the events and look for the modified/deleted records so that you update the back-end databse. What you need is to call the SubmitChanges() method of the context when you need the databse to be updated. LINQ is quite optimized to handle this pretty smoothly.
If you have not exposed all of the fields, you have to be carefull. For example, when you are hiding a field that cannot be null, you have to expose this or at least set this field's value in code behind when adding the record. In the NorthWindDataBase in the Products table, the ProductName cannot be null, and if you will get an exception if you do not set this and commit changes. If you want to set some data (for example for a property of the entity class from a hidden field) you can use the RecordUpdated event and check if the updated record is the CurrentAddRecord. I am going to include this in the sample as well. Editting and removing are pretty straightforward simply by commiting the changes.
Let me know if you have questions on this.