Avatar of Camillia
Camillia
Flag 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"
C#

Avatar of undefined
Last Comment
Camillia

8/22/2022 - Mon
burakiewicz

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();
Camillia

ASKER
oh, let me see.
Camillia

ASKER
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();


Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Camillia

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

but can I do this in one line or do i need to loop?
Camillia

ASKER
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
burakiewicz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
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();
                }
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
yes, your method is much better.Thanks