how to pass datagridview data from form2 to form1 combobox with keypress and selection row databounditem in vb.net

  Kiến thức lập trình

I’m Trying to pass datagridview data from form2 to form1 combobox with keypress enter twice and selection row databounditem in vb.net

Is it possible to do it or is there another method?

For Form2 I don’t want to use ShowDialog because I don’t want to close Form2

Please Guide me

Thanks

Code in Form1

 Public Class Form1
     Private CatProdService As New CatProdService()
     Private bindingSource1 As BindingSource = Nothing
 
     Private 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 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
         Form2.Show()
         End Using
     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

Code in Form2

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
    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 = txtDesCatProd.Text
})
        MessageBox.Show("Successfull")
        LoadData1()

Form1.BindcomboboxCatProd()
Catch ex As Exception
MessageBox.Show(ex.Message, “MYAPP”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT