I’m trying to do a dead simple thing but cannot find an effective way to do it. I have a WPF application that has a DataGrid. I want the DataGrid to show all the entries of a table (in a SQL Server CE 3.5 database). That part is done easily by setting the ItemsSource of the DataGrid.
Now I need the User to be able to add rows using the built-in functionality of just entering data in the last row of the grid.
My table has an Id column which is the PK of the table (it is not an Identity column because SQL Server CE 3.5 does not support it with EF). So I need to calculate the max Id everytime I enter a new item.
I’m struggling with the DataGrid events to achieve that simple thing but cannot find my way through. Anyone has an example on how to do that?
Thanks
You can use the
InitializingNewItemevent.InitializingNewItemEventArgshas a propertyNewItem(object) that you can cast to the type of your data and set its key value. You may want to get the max value once and increment it in memory.I assume this is the only place where new objects are created (as it is sql-ce, no concurrency), but if there is another place in your application where this could happen you’d need to ensure unique keys.