datagridview row with a combobox column not inserting in database
Hi!
I added a DataGridView to my windows form. The grid is data bound and one of it's columns is of type DataGridViewComboBoxColumn and the column is also data bound. Following is the code used for creating the grid and updating data to DB:
Private Sub Fill_CBGrid()
Dim colCB As New DataGridViewComboBoxColumn
colCB.FlatStyle = FlatStyle.Flat
colCB.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
colCB.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
colCB.HeaderText = "Brands"
If conSMS.ConnectSQLDB(My.Settings.SERVER, Schema, My.Settings.ISEC) = True Then
Try
dgCBrand.DataSource = dsCamp
dgCBrand.DataMember = "CAM.drCB"
dgCBrand.Columns.RemoveAt(2)
dgCBrand.Columns.Insert(2, colCB)
colCB.DataSource = dsBrands
colCB.Name = "cb_name"
colCB.DataPropertyName = "cb_name"
colCB.DisplayMember = "CB.cb_name"
dgCBrand.Columns(0).Visible = False
dgCBrand.Columns(1).Visible = False
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
Me.Close()
End Try
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If conSMS.ConnectSQLDB(My.Settings.SERVER, Schema, My.Settings.ISEC) = True Then
Try
BMB.EndCurrentEdit()
adpCampaign.Update(dsCamp.Tables(0))
adpCB.Update(dsCamp.Tables(1))
MsgBox("Campaign details saved successfully!", MsgBoxStyle.Information, "Campaign Details")
Catch ex As Exception
MsgBox("An error occurred while saving Campaign Details. Error is: " & ex.Message, MsgBoxStyle.Critical, "Campaign Details")
End Try
End If
End Sub
Now, if I select a value from drop down list of the combo box column, press enter key and then click on save button then my data is successfully updated to the database but if I just select the value and leave the datagrid and click on save button, no data is inserted in the DB.
I used datagrid.EndEdit() on datagrid_leave event but the problem is still there.