如何从对象列表中获取字符串值并将其添加到下拉列表中?

时间:2011-11-22 20:38:58

标签: c# asp.net

我想列出一份包含3个部分,员工ID,姓氏和名字的员工列表,并将其添加到显示姓氏,名字的下拉列表中。

到目前为止,我为员工创建了一个课程:

   public class Employee
{
    public int emp_Id;
    public string lastName;
    public string firstName;

    public Employee(int id, string last, string first)
    {
        this.emp_Id = id;
        this.lastName = last;
        this.firstName = first;
    }
}

并创建了一个填充列表:

private List<Employee> employeeList = new List<Employee>();

此列表从sql查询填充,然后按姓氏排序。

foreach (DataRow row in ds.Tables["EMPLOYEE_TABLE"].Rows)
        {
          employeeList.Add(new Employee(int.Parse(row["EMP_ID"].ToString()), 
          row["LAST_NAME"].ToString(), row["FIRST_NAME"].ToString()));
        }

 employeeList.Sort(delegate(Employee E1, Employee E2) { return E1.lastName.CompareTo(E2.lastName); });

并且到目前为止所做的一切都与我想要的完全一样,但我无法弄清楚如何使用列表中包含的姓氏和名字值填充下拉列表。

已编辑

代码以提高可读性

5 个答案:

答案 0 :(得分:3)

见下面的代码:

DropDownList ddl = new DropDownList();

ddl.DataSource = employeeList;
ddl.DataTextField = "fullName";
ddl.DataValueField = "emp_Id";

我还会修改您的课程以包含全名字段:

public class Employee
{
    public int emp_Id { get; set; }
    public string lastName { get; set; }
    public string firstName { get; set; }
    public string fullName
    {
        get
        { 
            return String.Format("{0} {1}", this.firstName, this.LastName);
        }
    }

    public Employee(int id, string last, string first)
    {
        this.emp_Id = id;
        this.lastName = last;
        this.firstName = first;
    }
}

答案 1 :(得分:1)

现有属性的示例:

<asp:DropDownList id="bla" runat="server"  />

bla.DataSource = employeeList;
bla.DataTextField = "firstName";
bla.DataValueField = "emp_Id"
bla.DataBind(); 

我推荐这个:

<asp:DropDownList id="bla" runat="server"  />

bla.DataSource = employeeList;
bla.DataTextField = "fullName";
bla.DataValueField = "emp_Id"
bla.DataBind();

public class Employee
{
    public int emp_Id;
    public string lastName;
    public string firstName;

    public string fullName get{ return firstName + " " + lastName;}

    public Employee(int id, string last, string first)
    {
        this.emp_Id = id;
        this.lastName = last;
        this.firstName = first;
    }
}

答案 2 :(得分:1)

您可以在类中添加一个额外的属性来保存3个值,并在绑定DropDownList时将其用作DataTextField:

班级代码

public class Employee
{
    public int emp_Id;
    public string lastName;
    public string firstName;
    public string Text
    {
        get { return this.ToString(); }
    }
    public Employee(int id, string last, string first)
    {
        this.emp_Id = id;
        this.lastName = last;
        this.firstName = first;
    }
    public override string ToString()
    {
        return lastName + " " + firstName + " " + emp_Id;
    }
}

HTML:

List<Employee> employees = new List<Employee>();
ddl.DataSource = employees;
ddl.DataValueField = "emp_Id";
ddl.DataTextField = "Text";
ddl.DataBind();
祝你好运!

答案 3 :(得分:0)

为什么不创建一个名为FullName的属性来获取“FirstName +''+ LastName”?那会给你一个领域而不是两个。

答案 4 :(得分:0)

如果您不想或不能修改Employee,您也可以尝试这些方法:

var data = employee.Select (x =>
    new KeyValuePair<int, string>(
        x.emp_Id,
        string.Format("{0}, {1}", x.lastName, x.firstName)
));

ddl.DataSource = data.ToList();
ddl.DataValueField = "Key";
ddl.DataTextField = "Value";
ddl.DataBind();

如果您的员工有不同的下拉菜单,有时会使用姓氏,有时首先使用名字,可能有或没有冒号,这也可能很有用介于两者之间...