[英]How I Do Filter Data in ComboBox Vb.Net
enter image description here I need help about that I have some country names in Combobox
.在此处输入图像描述我需要帮助,因为我在
Combobox
有一些国家/地区名称。 I want like a first show from (A) then show from (B) like alphabetic order.我想要从 (A) 开始的第一个节目,然后从 (B) 开始,像字母顺序一样。
Dim connection As New SqlConnection("Data Source=PC1-PC;Initial Catalog=Test2;Integrated Security=True")
Dim command As New SqlCommand("select * from Table_6", connection)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
ComboBox1.DataSource = table
ComboBox1.DisplayMember = "Country"
Build a list of your countries.建立您的国家/地区列表。 Then sort the list and add to combobox.
然后对列表进行排序并添加到组合框。
Private Sub OpCode()
Dim Countries As New List(Of String) From {"Cabo Verde", "Ecuador", "Haiti", "Iraq", "El Salvador", "Djibouti", "France", "Bahrain",
"Cameroon", "Guinea", "Albania", "Finland", "Italy", "Jordan", "Grenada", "Dominica"}
Countries.Sort()
ComboBox1.Items.AddRange(Countries.ToArray)
End Sub
EDIT编辑
OKay, I am calling the field you are interested in "CountryName"
.好的,我正在调用您感兴趣的字段
"CountryName"
。 I am assuming it is the second field in the table which is index 1
.我假设它是表中的第二个字段,即索引
1
。
The Using...End Using
statements ensure that your database objects are closed and disposed even if there is an error. Using...End Using
语句确保您的数据库对象即使出现错误也已关闭和处理。 You don't need a DataAdapter
;您不需要
DataAdapter
; just load the DataTable
.只需加载
DataTable
。
The best approach to getting the list sorted is to let the database do the work by adding an Order By
clause.对列表进行排序的最佳方法是通过添加
Order By
子句让数据库完成这项工作。
Private Function GetCountryNames() As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection("Data Source=PC1-PC;Initial Catalog=Test2;Integrated Security=True"),
cmd As New SqlCommand("Select * From Tabel_6 Order By CountryName;", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function
Private Sub FillSortedCountriesCombo()
ComboBox1.DataSource = GetCountryNames()
ComboBox1.DisplayMember = "CountryName"
End Sub
If, for some reason, you need to do the sort in code then...如果出于某种原因,您需要在代码中进行排序,那么...
Private Sub FillUnsortedCountriesCombo()
Dim dt = GetCountryNames()
Dim CountriesArray = (From row In dt.AsEnumerable
Order By row.Field(Of String)(1)
Select row("CountryName")).ToArray
ComboBox1.Items.AddRange(CountriesArray)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.