0

I'm Trying to refresh ComboBox from BindingList to BindingSource in VB.NET, but it didn't work.

Is there something wrong with my code?

Whether it can be reset via BindingSource or BindingList or with other methods.

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

So my post is to reset or refresh the datasource in the combobox

From the Recommended Links provided is send the data, but if I do a dropdown from the combobox again, the data will definitely not appear.

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()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "MYAPP", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
End Class

Result from code in file gif

Result from code in file gif

3
  • The following may be of interest: stackoverflow.com/a/69743297/10024425 Commented Jul 27, 2024 at 3:15
  • This question is similar to: Send data within Child Forms. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 27, 2024 at 3:16
  • @user246821, Thank you for your reply. So my post is to reset or refresh the datasource in the combobox. From the link you provided, send the data, but if I do a dropdown from the combobox again, the data will definitely not appear. Please Guide me Commented Jul 29, 2024 at 3:19

1 Answer 1

0

Problem solved after I commentout the code below


 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
If ComboBox1.DataSource IsNot Nothing Then Return
         BindcomboboxCatProd()
     End Sub

     Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If ComboBox1.DataSource IsNot Nothing Then Return
         BindcomboboxCatProd()
     End Sub
'in Form2 event button

Form1.BindcomboboxCatProd()

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.