I am populating a WinGrid with data that needs to be formatted. For example, suppose my phonenumber field is 1234567890, i want this displayed at (123) 456-7890. To populate the grid i'm using custom business objects, the collection of which are passed into the SetDataBinding method of the grid.
I handle the formatting via the InitializeRow event, calling the SetValue method of the cell that I want to have formatted.
What i'm finding is that this formatting is being persisted back to the business object, which is messing me up.
How can I allow formatted text to be displayed in the grid but prevent that data from being saved to the underlying object? I've seen references to some sort of EditMode but i'm not sure how that's used and when/where I should set a property.
There are a number of ways to do this. Which one is best for you depends on the needs of your application and whether or not the user is allowed to edit the data.
If the user can't edit the numbers, then I recommend that you use an unbound column. What you would do is handle the InitializeLayout event of the grid. Hide the "real" bound column of and add an unbound column in it's place. Then you just handle InitializeRow and populate the cell in the unbound column with the decrypted version of the value in the bound column.
If the user can edit the data, then you could use a DataFilter. The DataFilter will allow you to trap the value as it moves from the editor (the encrypted value) to the display (the decrypted value) and vice versa.
That's valuable in many cases, including the one I gave. I gave a simplified example but let me give you the real one I'm trying to solve.
The column I'm displaying are credit card numbers. They are encrypted in the database but should be shown decrypted.
To accomplish this I call the decrypt routine on the cell in the InitializeRow method to display that result. Unfortunately the decrypted result is persisted back to the datasource, which causes issues for me later when I save a "record"
So I was hoping there was some way of setting what the display value should be, without affecting the underlying data.
To get around this I make a copy of the data into a different collection and bind that to the grid, but this is a short-term fix and I'm hoping there is a better way.
If you set the Value on the cell, then it will be written to the underlying data source. If you just want to format the value for display without changing the actual value in the cell, you can use the Format property of the column. When the grid displays the cell value, it calls the ToString method on the value and passes in the format you specify. So the format can be any format supported by the data type of the column.
Standard Numeric Format Strings
Custom Numeric Format Strings