Hi kana250688,
Define the BindcomboboxCatProd as Public.
On BtnShowForm2_Click open the form using Show method instead as Dialog.
In Form2 BtnSave_Click event handler call the BindcomboboxCatProd public method.
You can call this in your DataGridView selected row keypress in keyboard with enter 2.
Code
Form1
Imports System.ComponentModel
Imports System.Data.OleDb
Imports Dapper
Public Class Form1
Private CatProdService As New CatProdService()
Private bindingSource1 As BindingSource = Nothing
Public Sub BindcomboboxCatProd()
'If ComboBox1.DataSource IsNot Nothing Then Return
Cursor.Current = Cursors.WaitCursor
ComboBox1.DisplayMember = "CatProd"
ComboBox1.ValueMember = "CatProd"
bindingSource1 = New BindingSource With {.DataSource = New BindingList(Of CatProd)(CType(CatProdService.GetByCatProd(), IList(Of CatProd)))}
ComboBox1.DataSource = bindingSource1
Cursor.Current = Cursors.Default
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
End Sub
Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
BindcomboboxCatProd()
End Sub
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
BindcomboboxCatProd()
End Sub
Private Sub BtnShowForm2_Click(sender As Object, e As EventArgs) Handles BtnShowForm2.Click
'Using frm = New Form2()
' If frm.ShowDialog() = DialogResult.OK Then
' End If
'End Using
Form2.Show()
End Sub
End Class
Public Class CatProd
Public Property ID() As Integer
Public Property CatProd() As String
Public Property DesCatProd() As String
End Class
Public Class CatProdService
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\sample.accdb;Persist Security Info=False;"
End Function
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = GetOledbConnectionString()
Public Sub New()
_conn = New OleDbConnection(_connectionString)
End Sub
Public Function GetByCatProd() As IEnumerable(Of CatProd)
Dim sql = $"SELECT CatProd FROM CatProd"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of CatProd)(sql).ToList()
End Using
End Function
Public Function SearchHelper() As IEnumerable(Of CatProd)
Dim sql = "SELECT * FROM CatProd"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of CatProd)(sql).ToList()
End Using
End Function
Public Sub InsertCatProd(ByVal Obj As CatProd)
Dim sql = $"INSERT INTO `CatProd` (`CatProd`,`DesCatProd`) VALUES ('{Obj.CatProd}','{Obj.DesCatProd}');"
Using _conn = New OleDbConnection(GetOledbConnectionString())
_conn.Execute(sql)
End Using
End Sub
End Class
Form2
Imports System.ComponentModel
Public Class Form2
Dim CatProdService As New CatProdService()
Private bindingSource1 As BindingSource = Nothing
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
LoadData1()
End Sub
Private Sub LoadData1()
Dim Stock = CatProdService.SearchHelper()
bindingSource1 = New BindingSource With {.DataSource = New BindingList(Of CatProd)(CType(CatProdService.SearchHelper(), IList(Of CatProd)))}
DataGridView1.DataSource = bindingSource1
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
Try
'insert new record
CatProdService.InsertCatProd(New CatProd() With {.CatProd = txtCatProd.Text, .DesCatProd = txtDescCatProd.Text})
MessageBox.Show("Successfull")
LoadData1()
' Refresh the ComboBox
Form1.BindcomboboxCatProd()
Catch ex As Exception
MessageBox.Show(ex.Message, "MYAPP", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
Screenshot