Advertisement
5_2007-2008 Data Structures #183587

Auto insert Guid Datatable

Ok, I ran into a problem, MS out of some odd wisdom didn't do as you would think with the defaultvalue of a data column. you would think it so simple right? In a database such as SQL server you can set the default value to a function, I can do this with a datacolumn too such as. column1.defaultvalue = system.guid.newguid Though this has an issue, if you use this in a grid you will see as you add new rows that the darn guid is the same. It's calling the newguid only once when setting up the table and it's holding that value static for the rest of the life of the datatable\datacolumn. This goes against common knowlege in how MS does things in other area's, such as the before mentioned SQL server using NewID() will get you a new ID each time. Keep in mind that even other funtions like Now() (which returns current date time) won't work, the defaultvalue will always be the same datetime. So I don't have a solution for the default, I have a different solution. Custom datatable and custom datacolumn classes. You will see in my custom column style that there is a new property called (autogenerateguid)if set to true then it will then set each new row with the correct new guid. Though this is all code based, it works for what I need it to do. It also features the use of an un-documented function and class. NewRowFromBuilder Function System.Data.DataRowBuilder Class I've seen other post's that show to use the rowchanging or positionchanged events in order to do the same thing, this example gets to the source of the newrow creation. What would be better is that if you assign a function that MS would call that function each time instead of this lame static method.

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
Upload
Původní komentáře (3)
Obnoveno z Wayback Machine