vb.net 清除组合框项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15469176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net clear combobox items
提问by Ikong
I have two comboboxes that are related. My problem is my combobox2 items will not reset/clear as i select item from combobox1. Instead it keeps adding/appending the new items in the bottom of combobox2. I tried ComboBox2.DataSource = Nothing
and ComboBox2.Items.Clear()
before adding the new items but it still not clear it.
我有两个相关的组合框。我的问题是我的组合框 2 项目不会在我从组合框 1 中选择项目时重置/清除。相反,它不断在组合框 2 的底部添加/附加新项目。我在添加新项目之前尝试过 ComboBox2.DataSource = Nothing
, ComboBox2.Items.Clear()
但仍然没有清除。
Private Sub ComboBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Validated
ComboBox2.DataSource = Nothing
ComboBox2.Items.Clear()
ComboBox2.Items.Remove(ComboBox2.DisplayMember)
sql = "select brgyname,idbrgy from barangay where idmun=" & ComboBox1.SelectedValue.ToString
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbBrgy")
ComboBox2.DataSource = ds.Tables("cbBrgy")
ComboBox2.DisplayMember = "brgyname"
End Sub
回答by andy
Don't do
不要做
ComboBox2.DataSource = Nothing
Just clear the items
只需清除项目
ComboBox2.Items.Clear()
回答by Rajaprabhu Aravindasamy
Instead of using
而不是使用
ComboBox2.DataSource = Nothing
and ComboBox2.Items.Clear()
ComboBox2.DataSource = Nothing
和 ComboBox2.Items.Clear()
Use, ds.Tables("cbBrgy").Rows.Clear()
用, ds.Tables("cbBrgy").Rows.Clear()
回答by Chinedu
Use
用
ds.Tables("cbBrgy").Rows.Clear()
Instead of
代替
ComboBox2.DataSource = Nothing
ComboBox2.Items.Clear()
But ensure that ds.Table("cbBrgy")
is not Null
before using that line
但请确保在使用该行之前ds.Table("cbBrgy")
不是Null
回答by ???ěxě?
Try this... Change your method to...
试试这个......改变你的方法......
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.Items.Clear()
sql = "select brgyname,idbrgy from barangay where idmun=" & ComboBox1.SelectedValue
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbBrgy")
ComboBox2.DataSource = ds.Tables("cbBrgy")
ComboBox2.DisplayMember = "brgyname"
End Sub
Thanks!
谢谢!
回答by user3323470
combobox1.SelectedIndex = -1
that is the best way
这是最好的方法
回答by user4789879
If Not IsNothing(ds.Tables("cbBrgy")) Then
ds.Tables("cbBrgy").clear
End If
sql = "select brgyname,idbrgy from barangay where idmun=" & ComboBox1.SelectedValue
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbBrgy")
ComboBox2.DataSource = ds.Tables("cbBrgy")
ComboBox2.DisplayMember = "brgyname"
Dispose the tables first before populating it again and then setting it again as DataSource.
在再次填充之前先处理表,然后再次将其设置为数据源。
回答by davis
First check if data source has a value. For example:
首先检查数据源是否有值。例如:
if isnothing(combobox1.Datasource) then
*write your code to populate here
end if.
回答by Karthik
follow this,
按照这个,
ComboBox.Text = Nothing
ComboBox.Text = 无
this is enough to clear the values in the Combo Box.
这足以清除组合框中的值。