Thursday, December 09, 2010

Iterating through Generic List in C#

Recently I need to loop through a Generic list and bind the result to a Combobox. Here is how you can do this in C#

List<CCDSDateResult> list = new List<CCDSDateResult>();
for (int i = 0; i < e.Result.Count; i++)
{
    if (e.Result.ToList()[i].ccds_date.ToString().Length > 0)
    {
        // Write your logic..to add item in the list
        list.Add(new CCDSDateResult(DateTime.Parse(e.Result.ToList()[i].ccds_date.ToString())));
    }
}
// adding a empty record to the list
list.Insert(0, new CCDSDateResult());
this.OrderComboBox.ItemsSource = list.ToList();

Hope this is useful. Thumbs up

No comments:

Post a Comment