I've got a grid that I'm applying a datasource to in code. I need to setup insert/update/delete commands. When I do one of these ops, which is the best event to attach the save code?
Thank you Sean for contacting us today.
Please refer to the following blog regarding advice for CRUD operations and the events you can hook and perform your save.
https://es.infragistics.com/community/blogs/b/kiril_matev/posts/implementing-crud-in-the-infragistics-xamdatagrid
Sure, but it looks like those rely on creating an actual datasource and having a model. I've just got a datatable being returned from the DB. And I'm using that in 100 places in the app so I can't change methods.
Well, this isn't updated all that often so it can be pushed to the DB after every new row. However, your point is taken. I'm more of a SQL guy than a C# guy so a lot of the typical methods are unknown to me. So if you've got a good example of doing this kind of thing in batch I'd love to see it.
That said, I'm probably doing something wrong cause I still can't access the row data.
private void GridLocations_AfterRowInsert(object sender, RowEventArgs e) { MessageBox.Show(e.Row.Cells["LocName"].Value.ToString()); }
You should use Before/AfterRowUpdate event. Check the row.IsAddRow property to determine if this is a new row being added or an existing row being edited.
If you want to retain what is in the add new row you can set grid.RowUpdateCancelAction = RowUpdateCancelAction.RetainDataAndActivation;
Well, I only want to save what's in the row because I don't know any other way. Access the row, assign each cell a var, send the insert stmt to the DB.
How should I be doing it? Again, if you've got an example that shows what I should be doing I'd love to see it. I've looked all over and I can see all types of things that can be done, but nowhere that shows how to simply save a new/updated row to a DB.
If the grid is bound to a DataTable, then the table will automatically be updated by the grid. You don't have to manually copy the data from the grid into the DataTable.
Typically, you would use a DataAdpater to get your data from the DataBase into the DataTable and to write the changes back to the DataBase from the DataTable. That part doesn't even involve the grid, so it's not something we document, but I'm sure there is documentation on the DataAdapter provided by Microsoft.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/updating-data-sources-with-dataadapters
Thanks for the assist Mike. Sean, you are probably interested in calling AcceptChanges on the DataTable, explained in the DataAdapter doc above.