How do I immediately check a new insert row in Silverlight 3 Datagrid?

I have a Silverlight 3 tool library with a DataGrid user control. This grid does not have direct access to WCF RIA Services object types, so I use reflection to add a new element when the user clicks on the grid when it is empty:

private void InsertEmptyRecord() { if (this._dataGrid.ItemsSource == null) return; Type[] typeParameters = this._dataGrid.ItemsSource.GetType().GetGenericArguments(); if (typeParameters.Count() > 0) { Type itemType = typeParameters[0]; object newItem = System.Activator.CreateInstance(itemType); Type sourceType = typeof(System.Windows.Ria.EntityCollection<>); Type genericType = sourceType.MakeGenericType(itemType); System.Reflection.MethodInfo addMethod = genericType.GetMethod("Add"); addMethod.Invoke(this._dataGrid.ItemsSource, new object[] { newItem }); // == Validate data here == } } 

This works, but I need to check it also after adding a new item. There are two ways to do this:

  • Make the user enter edit mode for the first cell of a new row in the grid. (This will force a check if they are still on the page.)
  • Power checks to start immediately when a new line (or when the grid loses focus.)

I was unable to get any of them to work. Tried this, but it selects only the line, does not force to perform checks:

 this._dataGrid.SelectedItem = newItem; System.ComponentModel.IEditableObject editableItem = newItem as System.ComponentModel.IEditableObject; if (editableItem != null) editableItem.BeginEdit(); 

Any suggestions?

+3
reflection datagrid wcf-ria-services
source share
1 answer

Just got this job thanks to some help on this issue .

I added the following to the "== Validate data here ==" section in the code above:

 DataGridRow newRow = this._dataGrid.ChildrenOfType<DataGridRow>().FirstOrDefault(); if (newRow != null) { newRow.Loaded += (sender, e) => { this._dataGrid.CurrentItem = newItem; this._dataGrid.BeginEdit(); }; } 

This forces the first cell to immediately go into edit mode.

+1
source share

All Articles