我尝试在 VB.NET 中将 ComboBox 从 BindingList 刷新到 BindingSource,但没有成功。
我的代码有问题吗?
是否可以通过BindingSource或BindingList或其他方法重置。
对于 Form2 我不想使用 ShowDialog 因为我不想关闭 Form2
所以我的帖子是重置或刷新组合框中的数据源
从提供的推荐链接发送数据,但如果我再次从组合框中进行下拉,数据肯定不会出现。
谢谢
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
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
gif 文件中代码的结果
注释掉下面的代码后问题解决了
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
```