绑定源更改后,datagridview中的行不再可编辑

时间:2014-09-24 10:57:19

标签: c# winforms entity-framework data-binding datagridview

我正在使用基于Windows Forms的C#设计一个应用程序,它允许我公司的员工为客户花费数小时的项目。数据保存在MS SQL Server 2014中,ADO.NET Entity Framework 6用于将其加载到应用程序。我正在使用Visual Studio 2013和EF DBContext Generator是用户。 到目前为止只考虑一种形式,它包含一个组合框和一个数据网格视图。在组合框中将选择员工,将在gridview记录中显示该员工。两个对象的数据源都设置为BindingSource对象。

此问题的关键表如下:

t_employee(PK:t_employee_id) - 来自这一行的行在组合框中 t_employee_project_task(PK: t_employee_project_task_id, FK: t_employee_id)
t_employee_project_task_week(PK: t_employee_project_task_week_id, FK: t_employee_project_task_id) - 此行中的行位于datagridview中 引用具有相同名称的列。

有更多的FK和表格,但它们似乎并不重要。

以下是表格的代码:

public partial class f_main : Form {
    ais_etEntities _context; //DbContext
    t_employee current_employee; //current employee, the one selected in combobox
    public f_main() {
        InitializeComponent();
    }

    private void f_main_Load(object sender, EventArgs e) {
        _context = new ais_etEntities();
        //t_employee loading
        _context.t_employee.Load();
        current_employee = _context.t_employee.Single(emp => emp.name == Environment.UserName);

        //t_employee_project_task_week loading
        _context.t_employee_project_task_week.Where(eptw => eptw.t_employee_project_task.t_employee.t_employee_id == current_employee.t_employee_id).Load();

        //binding
        this.t_employee_project_task_weekBindingSource.DataSource = _context.t_employee_project_task_week.Local.ToBindingList();
        this.t_employeeBindingSource.DataSource = _context.t_employee.Where(em => em.t_employee_id == current_employee.t_employee_id || em.t_delegate.Any(d => d.t_delegated_to_id == current_employee.t_employee_id)).ToList();

    }

    private void comboBox2_SelectionChangeCommitted(object sender, EventArgs e) {
        current_employee = (t_employee)this.comboBox2.SelectedItem;
        _context.t_employee_project_task_week.Where(eptw => eptw.t_employee_project_task.t_employee.t_employee_id == current_employee.t_employee_id).Load();
        this.t_employee_project_task_weekBindingSource.DataSource = _context.t_employee_project_task_week.Local.ToBindingList().Where(eptw => eptw.t_employee_project_task.t_employee.t_employee_id == current_employee.t_employee_id);


    }
}

我想使datagridview可编辑(添加,删除,编辑行)。第一个员工很容易 - 我只为一个员工加载到上下文t_employee_project_task_week行,并将datagridview绑定到_context.t_employee_project_task_week.Local.ToBindingList()

当员工在组合框中更改时,在SelectionChangeCommitied事件中,我为他加载行到上下文并需要更改t_employee_project_task_weekBindingSource,以便只显示一个选定员工的记录。我将绑定设置为_context.t_employee_project_task_week.Local.ToBindingList().Where(eptw => eptw.t_employee_project_task.t_employee.t_employee_id == current_employee.t_employee_id),我的datagridview不再可编辑。 “不可编辑”是指BindingNavigator上的“添加行”图标变为灰色(您可以单击它)。 BindingNavigator与datagridview绑定到相同的BindingSource。

我认为_context.t_employee_project_task_week.Local.ToBindingList().Where(eptw => eptw.t_employee_project_task.t_employee.t_employee_id == current_employee.t_employee_id)_context.t_employee_project_task_week.Local.ToBindingList()

返回的类型存在问题

主要问题:如何更改绑定并仍然可以添加和删除行? 小问题:按员工加载t_project_task_employee_week行员工的想法是否正确?我想继续使用ADO.NET EF,但绑定方法可以改变。

任何建议都将受到高度赞赏!

0 个答案:

没有答案