Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Looping thru a List to find the first and second items

I have a list like this:
                List<CustomerQ.Customer> items = customerQ.Execute();
                cbxCName.DataSource = items;
                cbxCName.DataBind();

I want to loop thru that List and assign the first and second items to the first and second item of that checkbox cbxCName:

cbxCName.Items[0].Text =  "first item column of Items"
cbxCName.Items[0].Value = "second item column of Items"
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

are you trying to bind a property to the text and a property to the value like this
List items = customerQ.Execute();
cbxCName.DataSource = items;          
cbxCName.DataTextField="ItemName"
cbxCName.DataValueField="ItemId"
cbxCName.DataBind();
Avatar of Camillia

ASKER

oh, let me see.
no, backto my orig question...how can I get the Items's first and second columns?
cbxCName.DataTextField="ItemName"
cbxCName.DataValueField="ItemId"

--what is IteName?what is ItemId?

*** Orig code has an itemdatabound like below. I want to replace that item databound code and do it the binding all in one. I want to remove the ItemDataBound.

e.Item.Text = ((CustomerQ.Customer)e.Item.DataItem).LastName.ToString();
e.Item.Value = ((CustomerQ.Customer)e.Item.DataItem).Id.ToString();


I can do :
items[0].id and items[0].lastname

but can I do this in one line or do i need to loop?
I tried this but I dont see anything in my combo box:

                List items = customerQuery.Execute();
                cbxCName.DataSource = items;
                cbxCName.DataTextField = items[0].LastName.ToString();
                cbxCName.DataValueField = items[0].Id.ToString();
                cbxCName.DataBind();
ASKER CERTIFIED SOLUTION
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me try that but this also worked:
 int i;
                for (i = 0; i< items.Count - 1; i++)
                {
                    cbxCName.Items[i].Text = items[i].LastName.ToString();
                    cbxCName.Items[i].Value = items[i].Id.ToString();
                }
yes, your method is much better.Thanks