WinForms:带有E.F.的DataGridView不显示新行

WinForms:带有E.F.的DataGridView不显示新行,winforms,entity-framework,datagridview,Winforms,Entity Framework,Datagridview,我有一个简单的WinForm和一个DataGridView,它用实体框架显示记录。 我的问题是,仅当DataGridView为空时,才会显示新行功能。 问题出在哪里 先谢谢你 路易斯 PS 下面是WinForm类的一些详细信息: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq

我有一个简单的WinForm和一个DataGridView,它用实体框架显示记录。 我的问题是,仅当DataGridView为空时,才会显示新行功能。 问题出在哪里

先谢谢你

路易斯

PS 下面是WinForm类的一些详细信息:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GestioneFormazione;

namespace MpFormazione
{
public partial class MpFormazione : Form
{
    public MpFormazione()
    {
        InitializeComponent();
        this.dataGridView1.CellClick += DataGridView1_CellClick;
        this.dataGridView1.AllowUserToAddRows = true;
        this.dataGridView1.VirtualMode = true;
        this.dataGridView1.AutoGenerateColumns = false;
    }

    private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == this.ButtonColumunIndex)
        {
            if (e.RowIndex == 0)
            {
                Cliente cliente = new Cliente();
                
                cliente.ID = Guid.NewGuid();
                if (dataGridView1.Rows[0].Cells[1].Value != null)
                    cliente.Nome = dataGridView1.Rows[0].Cells[1].Value.ToString();                    
                SaveToDb(cliente);
                
            }
        }

        
    }
    private void SaveToDb(Cliente cliente)
    {
        using (var context = new MPFORMAZIONEEntities())
        {
            context.Cliente.Add(cliente);
            context.SaveChanges();                
        }
            

    }
    private void clienteBindingSource_CurrentChanged(object sender, EventArgs e)
    {
        
    }
    private void ClienteBindingSource_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
    {            

    }
    private int ButtonColumunIndex
    {
        get
        {
            return 2;
        }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void MpFormazione_Load(object sender, EventArgs e)
    {
        using (var context = new MPFORMAZIONEEntities())
        {
            this.dataGridView1.DataSource = context.Cliente.ToList<Cliente>();                
        }
    }       
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Gestionformazine;
名称空间格式
{
公共部分类表格:表格
{
公共管理信息网()
{
初始化组件();
this.dataGridView1.CellClick+=dataGridView1\u CellClick;
this.dataGridView1.allowUserToAddress=true;
this.dataGridView1.VirtualMode=true;
this.dataGridView1.AutoGenerateColumns=false;
}
私有void DataGridView1_CellClick(对象发送者,DataGridViewCellEventArgs e)
{
如果(e.ColumnIndex==此.ButtonColuminIndex)
{
如果(e.RowIndex==0)
{
Cliente Cliente=新客户();
cliente.ID=Guid.NewGuid();
如果(dataGridView1.Rows[0]。单元格[1]。值!=null)
cliente.Nome=dataGridView1.Rows[0]。单元格[1]。Value.ToString();
SaveToDb(客户);
}
}
}
私有void SaveToDb(客户)
{
使用(var context=new-MPFORMAZIONEEntities())
{
context.Cliente.Add(Cliente);
SaveChanges();
}
}
私有void clientbindingsource\u CurrentChanged(对象发送方,事件参数e)
{
}
私有void clientbindingsource_AddingNew(对象发送方,System.ComponentModel.AddingNewEventArgs e)
{            
}
专用int按钮ColumUnindex
{
得到
{
返回2;
}
}
私有void dataGridView1\u CellContentClick(对象发送者,DataGridViewCellEventArgs e)
{
}
私有void MpFormazione\u加载(对象发送方、事件参数)
{
使用(var context=new-MPFORMAZIONEEntities())
{
this.dataGridView1.DataSource=context.Cliente.ToList();
}
}       
}
}


该页面仅包含此代码,以及“客户”和“Dipendente”实体的实体框架。

显示代码的相关部分这是packages.config我在插入C代码时遇到问题。即使缩进正确,它也总是告诉我你的问题有问题。键入代码时,无论使用哪种语言,每行代码都以四个空格开头。子弹之后发生了一些奇怪的事情。在这种情况下:在项目符号之后添加一些文本(例如空格),然后再添加代码