Combobox Empty Rows Prevention?

pointeman
pointeman used Ask the Experts™
on
Q. Anyone know how to fill a ComboBox without empty rows?

I'm using a dataset and sorting the Combobox like so:

    comboBox1.Sorted = true;

   foreach (DataRow row in dataSet1.Tables[0].Rows)
   {
        comboBox1.Items.Add(row["LastName"]);
   }          
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
How does this work? (I cannot recall if you use DbNull.Value or null here. But try this.


   foreach (DataRow row in dataSet1.Tables[0].Rows)
   {
      if(row["LastName"] != null && row["LastName"] != "")
        comboBox1.Items.Add(row["LastName"]);
   }          

Author

Commented:
This works quite well, thanks for the advice...

if (!string.IsNullOrEmpty(row["LastName"].ToString()))
     comboBox1.Items.Add(row["LastName"].ToString());

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial