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.
Hi Sean,
1) AfterRowInsert fires when the grid inserts a row into the data source. That will happen if you click the AddNew button or if you change something in the TemplateAddRow. That's just the creation of the row. The row isn't committed to the data source until the user leaves that row or the grid loses focus. At that time, Before/AfterRowUpdate will fire. You can use the IsAddRow property of the row to determine if this is an existing row whose data has changed or a new row.
2) You should be able to access the new values from the grid row in AfterRowUpdate. They may not all be correct in BeforeRowUpdate, because if there is a cell still in edit mode, then the value for that cell will not be written to the underlying data source until the user leaves the cell or the value is committed in some other way.
Could you be more specific about exactly what you are trying to achieve here? It's generally not good practice to update the back end (database) every time the user makes a change to the grid. The DataSet/DataTable pretty much expect you to do this in batches using the DataAdapter.
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.
Thanks for the assist Mike. Sean, you are probably interested in calling AcceptChanges on the DataTable, explained in the DataAdapter doc above.
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