The way a problem is approached can have some serious consequences. Take for example the task I had to add some data to a DataGrid in WPF and allow a user to select the rows. When the rows were selected I would capture the details and put them into a form where they could be edited.
The interesting issue I had was that by default, a WPF DataGrid adds an extra, empty row at the end of the result set, called NewItemPlaceholder. When I selected on it, the value was null and received an exception.
The below code row worked just fine, as long as the row had data in it. It crashed when a user selected on the NewItemPlaceholder row.
int value = Convert.ToInt32(((DataRowView)e.AddedItems)
I spent some time trying to find out how to capture if the row is null or not. However, the simple solution was to set the IsReadOnly attribute in the DataGrid to true. Then the NewItemPlaceholder went away, as shown below.
Had I known that the IsReadOnly is set to false and that the NewItemPlaceholder is added to the end of a collection, then I could have avoided the frustration of searching for some code that checked for null.
Overtime, and the fact that I am documenting as I go, will result in faster implementation and better solutions from now on. Keep learning and keep coding…