All I wanted to do was force data entered directly into a DataGrid into upper case. You would think this would be an easy thing to do. Far from it. It involved delving into things called ValueConverters, which are not that difficult to write but are a major pain in the butt to actually use.
Archive for the ‘Silverlight’ Category
If you are going to let your users edit data in a DataGrid, at some point you will want to allow them to add records to the grid. Adding a new row is simplicity itself, you just add a new item to the collection that is acting as your data source. Suppose we are working with the data source from my prior DataGrid article. We just add a new Account object to our Accounts collection, like this:
This adds a new row to the DataGrid but it doesn’t put the grid into edit mode with the new row as the currently selected row. Doing this is a bit more involved. First we need to set the currently selected row by setting MyGrid.SelectedIndex to the index of the Account object in our collection. For a newly added row, that would be Accounts.Count - 1. i.e. the last object in the collection. Then we need to set the currently selected column by setting MyGrid.CurrentColumn to the first column in the row. This will always be MyGrid.Columns(0). Then we need to put the grid into edit mode by calling MyGrid.BeginEdit(), which puts the currently selected cell into edit mode. Finally we need to give the grid focus by calling MyGrid.Focus(). The complete code to add a row and set the grid into edit mode looks like this:
' Add a new account to our list of accounts Accounts.Add(New Account()) ' Make the newly added account the current row of the grid MyGrid.SelectedIndex = Accounts.Count - 1 ' Make the ID field the current column of the grid MyGrid.CurrentColumn = MyGrid.Columns(0) ' Put the grid into editing mode MyGrid.BeginEdit() ' Give the grid control focus MyGrid.Focus()
It seems like nothing is ever easy with this stuff. Sigh.
Binding a data source to a WPF data grid is not exactly difficult, but it isn’t exactly easy or convenient either. The only way that I have found to do it, so far, involves a combination of code behind logic and manually editing the XAML. There are some short cut methods that can reduce the amount of XAML code that needs to be manually edited, but I find these short cut methods to be less than completely satisfactory.
I am just getting started with Silverlight. For my first application, I wrote a simple form that interacts with a WCF service for database access.
The first time I tried to access the WCF service from the Silverlight application I got a SecurityException. The error message that the exception gave was long and rambling and mentioned things like “cross domain access” and other fairly meaningless bafflegab. I spent a lot of time Googling for solutions to this problem and the results all had to do with tweaking the IIS security settings, which are irrelevant when using Visual Studio’s internal web server.