简体   繁体   中英

Retrieving information stored in a list c# ASP.NET

I am using the following code to store a person's information from a grid view row in a list:

public class PlayerInfo
{
    public string FirstName { get; set; }
    public string Email { get; set; }
    public string DaysLeft { get; set; }
}


protected void btn_Click(object sender, EventArgs e)
{
    List<PersonInfo> person_list = new List<PersonInfo>();

          foreach (GridViewRow row in my_gridview.Rows)
        {
            CheckBox cb = (CheckBox)(row.Cells[2].FindControl("CheckBox"));
            if (cb.Checked)
            {
                person_list.Add(new PlayerInfo() {FirstName=row.Cells[0].Text,Email=row.Cells[2].Text,DaysLeft=row.Cells[5].Text});
            }
            else
            {

            }
        }

etc etc..

}

So if I try and output person_list[0] this will not work as there are three items associated to each point in the list, so position 0 in the list contains the first name, email and days left. I can see the items are being stored properly when debugging in visual studio.

How would I retrieve each item individually from this list? For example being able to set the values to variables in a loop, so the email address would be assigned to an email variable, the first name to another etc? I have only ever dealt with normal arrays and have never come across a hierarchical list before.

Thanks in advance.

Iterate it using for like this

for(int i =0; i <person_list.Count; i++)
{
  string name =  person_list[i].FirstName;
  string email =  person_list[i].Email;
  string daysLeft =  person_list[i].DaysLeft;
}

First of all your list variable is local so if you want to access it outside of your class you need to declare it at class level.

Second as you are adding the PersonInfo objects in your list, you will get PersonInfo out of your list.

As far as iteration is concerned it can be done with any loop:

Example:

foreach(PersonInfo pInfo in person_list)
{
  // your code
} 

There are several ways to get the values from a list.

You can iterate using for or foreach:

for (var i=0; i<person_list.Count; i++) {
    var email=person_list[i].Email;
}

foreach(var person in person_list) {
    var email=person.Email;
}

Or you can access to the individual members by using their indexes:

var person=person_list[1];
if (person!=null) {
    var email=person.Email;
}

Or you can query the list using Linq :

var person=person_list.FirstOrDefault(p=>p.FirstName=="Jack");
if (person!=null) {
    var email=person.Email; 
}

Also, to access the items in the GridViewRow, you can use GridViewRow.DataItem

foreach (GridViewRow row in my_gridview.Rows)
{
    var cb = (CheckBox)(row.Cells[2].FindControl("CheckBox"));
    if (cb.Checked)
    {
        person_list.Add(row.DataItem as PlayerInfo);
    }
 }

A beautiful way of using Lists with GridViews is by Setting DataPropertyNames to the names of the properties in your custom List and using it as a DataSource ie

my_gridview.DataSource = person_list;

Don't forget to set my_gridview.AutoGenerateColumns = false; unless you want the columns to be generated automatically based on the properties in your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM