当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Is there a way to clear blank values from a listbox at runtime?

问题描述:

I pull data from a table in a database and assign it as a datasource to a listbox. If there is a blank item in the listbox, it shows up as something like this:

  1. All
  2. Red

  3. Green
  4. Blue

What is the best way to remove the blank in the listbox. If there is a blank, it is always in the second position. I was going to try to test to see if the second position contained blank text, but I am not sure how to do that.

Thanks,

XaiSoft

网友答案:

I'd say the best thing to do is to remove the blank item from your datasource before you bind it to the listbox.

How you do this depends on where your data's coming from and how much control you have over it.

You could add a view to the database that only returns non-blank items; if you're using LINQ to SQL you could modify your query so that it only returns non-blank items; you could copy the items into a List and remove the blank ones yourself (I guess there aren't too many items if you're putting them in a listbox).

网友答案:

Hook up an event to the data bind of the list box. If the data item is empty or has an invalid string, then do not add it, else go ahead an add it.

Or loop through the data source yourself, and add only what is needed.

网友答案:

I used

physicalChannelComboBox.Items.Remove("");

the problem is that will only clean one item, you need to do it the as many times as empty items you have...maybe not the best solution...but it works.

分享给朋友:
您可能感兴趣的文章:
随机阅读: