开发者

.NET - DataGridView - Updating to database when user adds a row

开发者 https://www.devze.com 2023-01-29 21:01 出处:网络
Hey guys. I\'m having an issue with using a datagridview for a user entering data that\'s saved to a database. Basically, I just want them to throw stuff into a row and then my code will insert the da

Hey guys. I'm having an issue with using a datagridview for a user entering data that's saved to a database. Basically, I just want them to throw stuff into a row and then my code will insert the data. I know there has to be some event for what I'm trying to and I just can't figure it out.

I tried the LeavingRow event, but the problem with that is that the final value hasn't updated when this event triggers. What I mean is, say we have a row with four columns, and the user has entered data into the first three columns. They enter info in the last column, then push down to enter a new row. The insert fails, because it thinks the last column is still empty.

I also tried UserAddsRow, but had that fail because it triggers as soon as the user starts typing to add a row, so there would be only a single character in the first column of the row I was trying to add.

Does anyone have suggestions for getting these, or some other event working for my purposes?

The two inelegant alternatives I'm weighing are a) stringing together some labels/textboxes and an add button or b) using the DataGridCell_ValueChanged event, by having some nested try/catch that will try to insert first, then if it fails try to update, and then do nothing if the update doe开发者_如何学JAVAsn't work. It's grossly inefficient, but I think it would get the job done since there's only going to be a couple dozen rows tops at one time in this datagridview.

Thank you for your time.


I typically use the RowValidated Event. I use this event because it allows for a whole row to be entered / edited before trying to save the changes.

When handling the event I get the rows that have been added, updated or deleted from the dataset (DataSet.GetChanges) and then perform the appropriate actions per type (Add, Update, Delete).

0

精彩评论

暂无评论...
验证码 换一张
取 消