0

现在我正在将三个列表与一个联合结合起来。我想用这些值填充一个 GridView,但这里的问题是我想在列表中分隔这些值。例如:

  1. Listitem [0] "名字 + " " + 姓氏"
  2. 清单1员工ID
  3. Listitem [2] "第二个雇员名字 + " " + 第二个雇员姓氏"
  4. Listitem [3] 第二个雇员employeeId

我希望它显示在我的 GridView 上。这是我目前的代码:

List<string> ulist = _searchEmpList.Union(_searchTechSkillList).Union(_searchAssignList).ToList();
string s = string.Empty;

for (int i = 0; i < ulist.Count; i++)
{
    s += ulist[i] + ",";
}

string[] split = s.Split(',');
DataRow dr = _dt.NewRow();
dr["Name"] = split[0]; //firstname + +lastname
dr["EmployeeId"] = split[1]; //employeeid

_dt.Rows.Add(dr);

Session["DataTableSearch"] = _dt;

GridViewSearchResults.DataSource = _dt;
GridViewSearchResults.DataBind();

在此处输入图像描述

4

2 回答 2

1

我真的不明白你为什么要这么麻烦。以下将做你说你想要的。这是否是你真正想要的,是一个不同的问题。

List<string> ulist = _searchEmpList.Union(_searchTechSkillList).Union(_searchAssignList).ToList();
int i = 0;
while (i < ulist.Count-1)
{
    DataRow dr = _dt.NewRow();
    dr["Name"] = ulist[i]; //firstname + +lastname
    dr["EmployeeId"] = ulist[i+1]; //employeeid
    _dt.Rows.Add(dr);
    ++i;
}

这与您要求的循环重复了原始代码中的逻辑。它是否会给你你认为应该的结果是一个悬而未决的问题。Union组合这两个列表并创建一个具有不同值的新列表。因此,如果两个列表中有任何重复值,它们只会在结果中显示一次。

此外,Union不保证最终列表中的项目顺序。

于 2013-05-21T13:24:02.170 回答
0

这是您要求的示例^^

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace simpleOO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var myEmployee = new List<Employee>();

            var fistEmployee = new Employee();

            fistEmployee.FirstName = "Filip";
            fistEmployee.LastName = "Göndek";
            fistEmployee.TechSkills.Add(new TechSkills(){Skill="Engish"});
            fistEmployee.Id=1;
            myEmployee.Add(fistEmployee);
            myEmployee.Add(new Employee()
                                           {
                                               Id = 2,
                                               FirstName = "Helmut",
                                               LastName = "Müller",
                                               TechSkills = new List<TechSkills>()
                                                                                {
                                                                                    new TechSkills(){Skill="Engish"},
                                                                                    new TechSkills(){Skill="Spain"}
                                                                                }
                                           }
                                           );

            var filteredList = myEmployee.Where(employee => employee.TechSkills.Any(skill => skill.Skill == "Spain")).ToList();

            var bList = new BindingList<Employee>(filteredList);
            dataGridView1.DataSource = bList;
        }




    }

    public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private List<TechSkills> techSkills = new List<TechSkills>();
        public List<TechSkills> TechSkills
        {
            get { return techSkills; }
            set { techSkills = value; }
        }

        // what ever more you need ...
    }

    public class TechSkills
    {
        public string Skill { get; set; }
    }
}

我知道它非常简单和肮脏,但我想你明白我想告诉你的;)

于 2013-05-21T14:06:06.430 回答