Combobox无法正确刷新如何解决这个问题

Combobox无法正确刷新如何解决这个问题

本文介绍了Combobox无法正确刷新如何解决这个问题? Vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据添加到我的组合框中,但有时显示数据不显示(或刷新显示到我的组合框中)。我必须为之前输入的数据添加另一个数据才能显示。我怎么能对此完全刷新?



我尝试过:



 Private Sub BtnBlood_Click(sender As Object,e As EventArgs)处理BtnBlood.Click 
如果ComboBlood.Text =那么
MsgBox(请先输入值)
退出Sub
结束如果
尝试
Bloodforbtn()
Catch ex As Exception
MsgBox(ex.ToString)
结束尝试
End Sub
Private Sub Bloodforbtn()

如果MessageBox.Show(你确定要添加它吗?& vbNewLine&[Blood Type]: & ComboBlood.Text,保存数据,MessageBoxButtons.YesNo,MessageBoxIcon.Question)= Windows.Forms.DialogResult.Yes然后
Dim connString As New OleDbConnection(Provider = Microsoft.ACE.OLEDB.12.0;数据源= | DataDirectory | \ DB_HR.accdb; Jet OLEDB:DATABASE PASSWORD = dbhr123;)
connString.Open()

Dim da As New OleDbDataAdapter()
Dim dt As New DataTable()

Dim str As String
str =Insert into tblBloodtype([Bloodtype])Values(?)
Dim cmd As New OleDbCommand(str,connString)

cmd.Parameters.Add(New OleDbParameter(Bloodtype,CType(ComboBlood.Text,String)))

cmd.ExecuteNonQuery()
cmd.Dispose()

MessageBox.Show(ComboBlood.Text +已添加)
'ComboBlood.Text =
'显示数据
da.SelectCommand =新OleDbCommand(选择*来自tblBloodtype,myConnection)
da.Fill(dt)
ComboBlood.DataSource = dt
ComboBlood .DisplayMember =Bloodtype
connString.Close()

Else
退出Sub
结束如果
ComboBlood.Text =
结束次
解决方案

一些事情要尝试



- 在重新分配数据源之前尝试清除项目(我似乎记得做过类似 ComboBlood.DataSource =没什么



- 或者尝试重置数据绑定 []

 ComboBlood.DataSource = dt 
ComboBlood.DisplayMember = Bloodtype
ComboBlood.ResetBindings()


I am adding data into my combobox but sometimes the display data don't display(or refresh to display into my combobox). I have to add another data for the previous inputed data to display. How can I put a totally refresh on this?

What I have tried:

Private Sub BtnBlood_Click(sender As Object, e As EventArgs) Handles BtnBlood.Click
       If ComboBlood.Text = "" Then
           MsgBox("Please Input a Value first")
           Exit Sub
       End If
       Try
           Bloodforbtn()
       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
   End Sub
   Private Sub Bloodforbtn()

       If MessageBox.Show("Are you sure you want to add this?" & vbNewLine & "[Blood Type]:  " & ComboBlood.Text, "Saving data", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
           Dim connString As New OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB_HR.accdb;Jet OLEDB:DATABASE PASSWORD=dbhr123;")
           connString.Open()

           Dim da As New OleDbDataAdapter()
           Dim dt As New DataTable()

           Dim str As String
           str = "Insert Into tblBloodtype([Bloodtype]) Values (?)"
           Dim cmd As New OleDbCommand(str, connString)

           cmd.Parameters.Add(New OleDbParameter("Bloodtype", CType(ComboBlood.Text, String)))

           cmd.ExecuteNonQuery()
           cmd.Dispose()

           MessageBox.Show(ComboBlood.Text + " was added")
           'ComboBlood.Text = ""
           'DISPLAY THE DATA
           da.SelectCommand = New OleDbCommand("Select * from tblBloodtype", myConnection)
           da.Fill(dt)
           ComboBlood.DataSource = dt
           ComboBlood.DisplayMember = "Bloodtype"
           connString.Close()

       Else
           Exit Sub
       End If
       ComboBlood.Text = ""
   End Sub
解决方案
Couple of things to try

- try clearing the items before reassigning the datasource (I seem to remember doing something like ComboBlood.DataSource = Nothing

- Or try resetting the data bindings Control.ResetBindings Method (System.Windows.Forms)[^]

ComboBlood.DataSource = dt
ComboBlood.DisplayMember = "Bloodtype"
ComboBlood.ResetBindings()


这篇关于Combobox无法正确刷新如何解决这个问题? Vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:29