3

My problem:
I have a DataSet with multiple DataTables in it. All of these DataTables have a DataColumn named "Id" with a DataType of System.Guid.

A short example of such a table:

+--------------------+
| User               |
+--------------------+
| *Id as System.Guid |
| Username as String |
| /* more columns */ |
+--------------------+

I assigned this DataTable to a DataGridView. The DataGridView is configured to allow creating, editing and deletion of entries.

When a user now creates a new entry and saves it, there is an exception that the "Id" is unset.

My question:
How is it possible to automatically generate a new System.Guid for new entries?

My current thoughts:

  • Specifying some magic value in dataSet.dataTable.Columns['Id'].DefaulValue . (Something like the <DBNull>)
  • Using something like a trigger on dataSet.dataTable listening for new rows. It would have to get executed before the row gets validated
  • Same thing like the trigger, but on the dataGridView.

What I've tried:

  • These events of the DataGridView: RowsAdded, UserAddedRow, RowValidating, DefaultValuesNeeded

It would be awesome if anyone would have a working solution or a hint for me!

~Chris

cimnine
  • 3,987
  • 4
  • 33
  • 49
  • But if the user adds new row to the DataTable, I immagine it does in code by using some function AddRow(DataRow row), what if just in that function put the code that injects unique ID value? – Tigran Jul 13 '11 at 10:13

3 Answers3

6

I'm not sure why you say you cannot use DefaultValuesNeeded - I've just tested this and it works perfectly:

void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    e.Row.Cells["Guid"].Value = Guid.NewGuid();
}

I set the visibility property of the Guid column to false so that it doesn't show. When a new row is created the row has a guid populated.

David Hall
  • 32,624
  • 10
  • 90
  • 127
  • Yeah I guess that way it would worked.. But I did not have the guid column defined in the view, so it wasn't available. – cimnine Jul 17 '11 at 09:56
  • @cimnine yes, so instead of removing to column from the view, leave it there and hide the colummn instead. Then you should be fine. – David Hall Jul 17 '11 at 10:01
  • I'll do that next time I guess. This time I added a hook in the `DataTable` definition returning always a proper initialized `DataRow`. Looks pretty elegant, but it isn't obvious where the new `System.Guid` comes from, so I would prefere the `DataGridView` hook next time. – cimnine Jul 18 '11 at 18:29
1

Yeah, just use the appropriate event and add the GUID value to the correct column. If the column is visible to the user then you could add it when the user clicks in one of the cells for the new row. Perhaps the RowsAdded event will do the job

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded.aspx

musefan
  • 47,875
  • 21
  • 135
  • 185
  • I guess that is a start.. I'll give it a shot. Thanks for now! – cimnine Jul 13 '11 at 10:17
  • It was a false start ;) The `RowsAdded` event occurs when rows are added to the `DataGridView` in general, including from the `dataGridView.DataSource`. It is not restricted to truly 'new' rows. But there is the `UserAddedRow` event, which I somehow missed till now. I'll see ^^ – cimnine Jul 13 '11 at 10:23
  • Oh yeah, I missed that one too, looks promising – musefan Jul 13 '11 at 10:54
  • But it didn't work out because no `DataRow` exists for that row at the time when the event gets triggered. And I don't show the 'Id'-column in the `DataGridView`, so I can't even use the `DefaultValuesNeeded` event -.-* – cimnine Jul 13 '11 at 11:07
0

I was hoping to use the DefaulValue to simplify things and replace my current solution, but after reading this thread it looks like I cannot.

My current solution is to have the [ID] column in the DataGridView (Visible = false) and add the new GUID if it is missing on the DataGridView RowValidating event.

    private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
        try
        {
            //If ID is empty create a new GUID
            Guid guidID = Guid.NewGuid();
            if (this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() == "")
                this.dataGridView1.Rows[e.RowIndex].Cells[2].Value = guidID;

        }
        catch { }
    }