填充datagridview winforms .net 4.0

时间:2015-02-28 17:18:07

标签: c# datagridview

我在使用2列加载datagridview时遇到问题。 使用Datagridview和1个按钮的表单。

部门(文字) EmployeesByDpt(组合)

我有一个带有datagridview和Button(Load)的表单,当我按下load时,应该填充datagridview。 单击Employee Combo应显示属于特定部门的所有员工。

我似乎无法让它工作,下面就是我所做的,

有什么建议吗?目前没有任何显示。谢谢

代码(为了简单起见,我把所有内容放在一起)

     public partial class Form2 : Form
{
    Repository repository;
    readonly DataGridViewTextBoxColumn colDepartment=new DataGridViewTextBoxColumn();
    readonly DataGridViewComboBoxColumn colComboEmployeesByDpt = new DataGridViewComboBoxColumn();
    public Form2()
    {
        InitializeComponent();
        repository = new Repository();
        SetupDataGridView();
    }

    private void SetupDataGridView()
    {
        dataGridView1.EditingControlShowing += OnEditingControlShowing;
        dataGridView1.CellValueChanged += OnCellsValueChanged;
        dataGridView1.AutoGenerateColumns = false;

        colDepartment.DataPropertyName = "Name";
        colDepartment.HeaderText = "Department Name";

        colComboEmployeesByDpt.DataPropertyName = "Employees";
        colComboEmployeesByDpt.HeaderText = "Employees";
        colComboEmployeesByDpt.DisplayMember = "FullName";
        //colComboEmployeesByDpt.DataSource = "FullName";


        dataGridView1.Columns.AddRange(new DataGridViewColumn[] { colDepartment ,colComboEmployeesByDpt});
    }

    private void OnCellsValueChanged(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == colDepartment.Index)
        {
            var control = e.Control as DataGridViewComboBoxEditingControl;
            if (control != null)
            {
                var bs = control.DataSource as BindingSource;
                if (bs != null)
                {
                    var comboBox = e.Control as ComboBox;
                    BindingList<Employee> employees = repository.GetEmployeeByDepartments(control.Text);
                    comboBox.DataSource = employees;
                    object employeeValue = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[colComboEmployeesByDpt.Index].Value;
                    if (employeeValue == DBNull.Value || employeeValue == null)

                    if (dataGridView1.CurrentCell.Value != DBNull.Value && dataGridView1.CurrentCell.Value != null)
                    {
                        control.SelectedValue = dataGridView1.CurrentCell.Value;
                    }
                }
            }
        }
    }

    private void btnLoad_Click(object sender, EventArgs e)
    {

        BindingList<Department> departments = repository.GetDepartments();

        dataGridView1.DataSource = departments;
        dataGridView1.Refresh();

    }
}

public class Department
{
    public Department()
    {
        Employees=new BindingList<Employee>();
    }
    public string Name { get; set; }
    public BindingList<Employee> Employees { get; set; }
}

public class Employee
{
    public string FullName { get; set; }
}

public class Repository
{
    public BindingList<Department> GetDepartments()
    {
        var departments=new BindingList<Department>();
        departments.Add(new Department{Name = "Food"});
        departments.Add(new Department{Name = "Travel"});
        departments.Add(new Department{Name = "Beauty"});
        return departments;
    }
    public BindingList<Employee> GetEmployeeByDepartments(string name)
    {
        var employees = new BindingList<Employee>();
        switch (name)
        {
            case "Food":
                employees.Add(new Employee { FullName = "Jim Bloggs1" });
                employees.Add(new Employee { FullName = "Jim Bloggs2" });
                break;
            case "Travel":
                employees.Add(new Employee { FullName = "Marc Smith1" });
                employees.Add(new Employee { FullName = "Marc Smith2" });
                break;
            case "Beauty":
                  employees.Add(new Employee { FullName = "Mario XXX1" });
                  employees.Add(new Employee { FullName = "Mario XXX2" });
                break;

        }

        return employees;
    }
}

1 个答案:

答案 0 :(得分:0)

运行您的确切代码,我遇到了几个问题。以下说明将向您展示我为解决每个后续问题所做的工作,但有一个警告:最后,我无法从ComboBox进行选择而不更改DataGridViewDepartments之间的绑定方法colComboEmployeesByDpt.DataPropertyName = "Employees"; 并删除了一些提供的代码。

每行抛出ArgumentException

每一行都抛出异常:“DataGridViewComboBoxCell值无效。”更改以下行修复此问题:

colComboEmployeesByDpt.DataPropertyName = "Employee";

OnEditingControlShowing

清空组合框

现在你会注意到ComboBox都是空的。在事件处理程序if statement中,第一个colComboEmployeesByDpt.Index应检查colDepartment.Index而不是if (bs != null)。但这还不够,因为control.Text总是假的。即使修复该检查,BindingList<Employee> employees = repository.GetEmployeeByDepartments(this.dataGridView1.CurrentRow.Cells[colDepartment.Index].Value.ToString()); 也始终为空。相反,尝试:

ComboBox

有了这个,您将看到每个ArgumentException都有正确的员工姓名列表。但是,ComboBox已经返回。尽我所能,这次我无法修复它。 (我怀疑colComboEmployeesByDpt.DisplayMember = "FullName"; private void OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ... } 项目列表始终为空,因此所选值为“无效”。)

答案 - 重组

为了让它发挥作用我做了几个核心的改变。我完全放弃了以下内容:

OnCellsValueChanged

然后我添加了一个公共属性来记住Departments,手动绑定每一行的员工,并在部门名称更改时连接BindingList<Department> Departments { get; set; } private void OnCellsValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == colDepartment.Index) { this.Departments[e.RowIndex].Employees = repository.GetEmployeeByDepartments(this.dataGridView1.CurrentCell.EditedFormattedValue.ToString()); DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)this.dataGridView1.CurrentRow.Cells[colComboEmployeesByDpt.Index]; cell.DataSource = this.Departments[e.RowIndex].Employees; } } private void btnLoad_Click(object sender, EventArgs e) { //this.dataGridView1.Rows.Clear(); // Needed if the button can be clicked repeatedly. this.Departments = repository.GetDepartments(); foreach (Department department in this.Departments) { department.Employees = repository.GetEmployeeByDepartments(department.Name); DataGridViewRow row = (DataGridViewRow)(dataGridView1.Rows[0].Clone()); DataGridViewTextBoxCell textCell = (DataGridViewTextBoxCell)(row.Cells[0]); textCell.Value = department.Name; DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)(row.Cells[1]); comboCell.DataSource = department.Employees; comboCell.DisplayMember = "FullName"; dataGridView1.Rows.Add(row); } } 以刷新列表:

{{1}}

这个解决方案对我有用。当我有空闲时间时,我将继续从困难的地方开始研究修复原始解决方案。希望现在有所帮助。