This will be my last Silverlight 2.0 Immersity post because we've arrived at the portion of our show where it's all .NET all the time. Now that the datagrid is pretty much in place, we're back to supplying the Business Logic to populate lists, update, insert and delete data on the back end of the Web Service, same-old-same-old. Then again, this is the beauty of Silverlight 2.0 as I always anticipated it since my first look at ReMix07 Boston. After some initial orientation, it's just .NET, the same stuff we do every day.
Friday's task was inserting and deleting records in the datagrid, which we'll touch on briefly. I'm going solely with the "Delete" and "Insert" keys with no additional buttons, so using the DataGrid KeyDown event, here is the bulk of the logic.
private void dg_KeyDown(object sender, KeyEventArgs e)
if (!editing && e.Key == Key.Delete && dg.SelectedItem != null)
else if (e.Key == Key.Insert)
LaborPost post = PopulateNewLaborPost();
int index = laborPosts.IndexOf(dg.SelectedItem as LaborPost) + 1;
dg.SelectedItem = index;
If you need more control of insertion and deletion (limiting the number of rows added, add rows automatically on data entry, etc), creating a derived class of ObservableCollection for the DataGrid data source would give that to you. This is demonstrated in Mike Taulty's DataGrid excellent screencast. The code accompanying that screencast is not available for download, but it's still useful in demonstrating the technique. Speaking of deriving from ObservableCollection, here is a good Silverlight.net discussion demonstrating the creation of a "SuperObservableCollection" to support a CommitingEdit event.
Remember how I mentioned my inability to set a ComboBox default? It dawned on me after using .IndexOf(dg.SelectedItem...) to determine the current DataGrid row for insertion and deletion that I could use the same approach to set the default value in a Silverlight 2.0 ComboBox. Here is how I'm doing that. (I had a better way of determining the current column, but I overwrote a day's work due to a bass-ackwards laptop drag-n-drop backup and forgot my original logic.)
private void dg_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
if ((string)dg.CurrentColumn.Header == "Phase")
MyComboBox phases = (MyComboBox)dg.CurrentColumn.GetCellContent(e.Row);
phases.ItemsSource = laborPhases;
LaborPhase phase = (from c in laborPhases
where c.PhaseID == currentLaborPost.PhaseID
int index = laborPhases.IndexOf(phase);
phases.SelectedIndex = index;
I spent all of this past week working on my laptop from the futon of our sunroom because I wasn't able to install Silverlight Tools for Visual Studio 2008 on my main development machine. My development machine is not directly connected to the Internet and the Silverlight 2.0 Tools install includes a dependency download process. I discovered the /CreateLayout Silverlight_Tools.exe command line option today, so if that works I'll be back in my office starting on Monday to continue on with the everyday .NET coding the week of Silverlight 2.0 Immersivity has made possible. That's okay. Development should feel like work on weekdays. Or should it?.....