This note is included to address the incorrect behavior of the Dataset Designer that can cause new inserts to fail in the datagridview or when attempting to correct the issue within the designer in the way that makes sense, you get the following error from the dataset designer upon a save
Custom tool error: Failed to generate code. Failed to generate code. Object of type 'System.Guid' cannot be converted to type 'System.String'. Object of type 'System.Guid' cannot be converted to type 'System.String'
Here is the scenario, A table is defined that has a UniqueIdentifier row, defined as the table's RowGuid and set as the primary key, and the New Value is set to "NewId()" in the table definition; thus upon insert a new guid will be generated and assigned to the table.
You create a new DataSet, drag the table onto the designer. The RowGuid is included in the resulting dataset. Since it is the primary key the dataset marks it as Null not allowed, raise an error during validation if Null and finally the default value will be set to <DBNull>.
Now you don't want to show the Guid to the end users, because they don't want to know about them and you especially don't want to require them to have to enter a new one. On a form you place a DataGridView, and you bind it to your dataset, and you allow insert from the grid, and you remove the Guid column, knowning the database will create one for you upon insert.
When you try to add a row thru the DataGridView, however you will get an error on insert due to the fact the Guid is NULL and that is invalid. So the next thing you try is to allow nulls in the Dataset Designer, that will fail. The next thing to try is specify a default value for the Guid in the designer. This is where you get the error. If you attempt to add a string "00000000-0000-0000-0000-000000000000" for example you will get an error setting that property that the value is of the incorrect type. If you then attempt to add the value without the quotes -- 00000000-0000-0000-0000-000000000000 -- the designer accepts the value, and then when you try to save your changes the error list in VS tells you that:
Error 33 Custom tool error: Failed to generate code. Failed to generate code. Object of type 'System.Guid' cannot be converted to type 'System.String'. Object of type 'System.Guid' cannot be converted to type 'System.String'. C:\Users\<username>\Documents\Visual Studio 2008\Projects\databinding\DataBindingWinForm\DVDListDataSet.xsd 1 1
OK, so you can't leave it null, you can't set the default value to a string representation, and you can't set the default value to a literal representation... What do you do? Making the guid visible and making you user specify one doesn't seem like a good idea to me.
You can address the problem by adding code to the TableNewRow event of the dataset's table.
in the Form1_Load event add a new handler, to that event and then in the event, set the Guid's value like this
private void Form1_Load ( object sender , EventArgs e )
{
// TODO: This line of code loads data into the 'dVDListDataSet.DVDs' table. You can move, or remove it, as needed.
this.dVDsTableAdapter.Fill( this.dVDListDataSet.DVDs );
this.dVDListDataSet.DVDs.TableNewRow += new DataTableNewRowEventHandler( DVDs_TableNewRow );
}
void DVDs_TableNewRow ( object sender , DataTableNewRowEventArgs e )
{
e.Row.SetField<Guid>( "Uid" , Guid.NewGuid( ) );
}
Another way of achieving the above would be to add a partial class to the dataset.
For example :-
Partial Class MyDatabaseDataSet
Partial Class jobsDataTable
Private Sub newJobsDataTableRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) Handles MyClass.TableNewRow
e.Row.SetField(rowguidColumn, Guid.NewGuid)
End Sub
End Class
End Class
This has the advantage of setting the value for the guid column wherever it is used rather than just in the form where the event handler is added.