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

3

我希望能够创建一个包含员工编号、姓和名的列表,并将其添加到下拉列表中,以显示姓、名。

目前为止,我已经为员工创建了一个类:

   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); });

到目前为止,一切都按照我的意愿进行,但我无法弄清楚如何将下拉列表填充为包含在列表中的姓和名字值。

代码已编辑以提高可读性


3
除非有特殊原因,我建议在SQL查询中对员工进行排序。 - marapet
5个回答

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;
    }
}

感谢您进行了澄清编辑。这正是我所寻找的。完美。 - Spacemancraig

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;
    }
}

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

祝你好运!


0
为什么不创建一个名为FullName的属性来获取“FirstName + ' ' + LastName”?这将使您只需处理一个字段,而不是两个。

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

如果您有不同的页面为员工提供不同的下拉菜单,有时以姓氏为先,有时以名字为先,可能还有带或不带冒号的情况,那么这也可能很有用...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接