开发者

How to avoid getting empty fields in a databound combo box?

How to avoid getting empty fields in a开发者_运维技巧 databound combo box?


If you are using a List(of ..) to hold the values of your ComboBox text then you can try:

Dim idList1 As New List(Of String)
idList1.Add("1")
idList1.Add("2")
idList1.Add("3")
idList1.Add("1")

Dim idList2 As New List(Of String)
idList2 = idList1.Distinct().ToList()

The idList.Distinct() pulls out the unique values. So that if you were to print each item in idList2 you would just get ("1","2","3"). Do this first before you create the contents of the ComboBox.


If you can't filter the data you recieve before it gets to you, you can create a new DataView and apply your own filter and use that as your datasource.

Dim i As Integer = 0
Dim table As DataTable = New DataTable
table.Columns.Add("id", GetType(System.Int32))
table.Columns.Add("name", GetType(System.String))
i = (i + 1)
table.Rows.Add(i, "hello")
i = (i + 1)
table.Rows.Add(i, string.Empty)
i = (i + 1)
table.Rows.Add(i, "what")
i = (i + 1)
table.Rows.Add(i, DBNull.Value)
i = (i + 1)
table.Rows.Add(i, "derp")
Dim view As DataView = New DataView(table)
view.RowFilter = "name <> ''"
Me.comboBox1.ValueMember = "id"
Me.comboBox1.DisplayMember = "name"
Me.comboBox1.DataSource = view

RowFilter takes a sql-style where clause for selecting matches. this includes only the 3 good values in the combobox.

selecting only the data you need in the first place is still a better idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜