How do you fill out a particular column of a DataTable (say, a uniqueidentifier) on creation of a row so that you can always assume that column is going to have a useful value?
I’ve got this column bound to a property on an object of mine with
NullValue = Guid.NewGuid()
but it requires FormattingEnabled and so I suspect that this isn’t really working (property’s still handy, thankfully). I don’t want to have to sprinkle
row[UniqueID] = Guid.NewGuid();
statements everywhere I’m creating a new row – I’d prefer to do it once, then rest easy knowing that every time I create a new row, it’ll have a uniqueidentifier.
You should catch the TableNewRow event on the DataTable, which is fired every time you create a new DataRow using the NewRow() method. In the event handler you can prepopulate all the columns you need to have “default” values.
Note that default values don’t flow through from the database, so don’t rely on a
uniqueidentifiercolumn having a default value ofnewid()– it doesn’t mean that new rows in your DataTable will get Guid.NewGuid() as their default values. That’s what TableNewRow is for.