winzip icon

Auto insert Guid Datatable

Submitted on: 1/2/2015 9:49:00 AM
By: Jose Fuentes VB MVP (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: VB.NET
Views: 4977
     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.
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Other 18 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.