Вопрос

This is a VB.NET winforms project, using EF. The ComboBox is bound to a seperate datasource which contains the colums tName and tNumber. TName is a brief description of that value and tNumber is the actual number that is saved in the Financial Table under column named transaction_type. Everything works flawlessly on the display of existing data that is in the Financial Table.

The comboboxes for each item in the grid all show the correct description for the transaction_type. But when ever I change the value of any of the combo boxes and click the save button it does not save any value to the transaction_type.

Any ideas why this might be? Any missing code I will add if required..

The form_load event looks like this:

    Private Sub paymentHistoryView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    FinancialDataGridView.DataSource = db.financials.Where(Function(f) f.TenantId = tenentId).OrderBy(Function(f) f.transaction_date)
    TranstypeBindingSource.DataSource = db.transtypes.OrderBy(Function(f) f.tNumber)
    BindingNavigatorDeleteItem.Enabled = False

End Sub

And the savebutton click event is as follows:

Private Sub FinancialBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FinancialBindingNavigatorSaveItem.Click
    db.SaveChanges()
End Sub

The properties for the ComboBox are shown Below:

ComboBoxProperties

It should be noted that all other changes to the datagrid are saved correctly when the save button is clicked... After further testing the value will actually save if the ComboBox is no longer selected. I guess a work around would be to focus on something else after the value of a comboBox is changed. If this seems like the best way how would I hook on SelectedIndexChanged event for comboBoxs in the datagridview???

Это было полезно?

Решение

I think that if you check, the same thing will actually happen with the other columns in the datagridview, if you do not move out of the cell before clicking the save button. The good news is that you only need to add one line of code.

FinancialDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)

Now your SaveItem_Click event handler should look like this:

Private Sub FinancialBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FinancialBindingNavigatorSaveItem.Click
    FinancialDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
    db.SaveChanges()
End Sub

This will commit the dirty cell before the save.

See https://stackoverflow.com/a/6469559/269123

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top