WinForms: DataGridView does not show Insert new record

  • Thread starter Thread starter Ciupaz
  • Start date Start date
C

Ciupaz

Guest
Hello,
I have a simple WinForm with a DataGridView that shows record from a table with Entity Framework (6).

My problem is that appear the Insert New Record button (the asterisk) only if the grid is empty,
and not when has some records.

Why this appears?

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 int ButtonColumunIndex
{
get
{
return 2;
}
}


private void MpFormazione_Load(object sender, EventArgs e)
{
using (var context = new MPFORMAZIONEEntities())
{
this.dataGridView1.DataSource = context.Cliente.ToList<Cliente>();
}
}
}




Thank you in advance.


Luis

Continue reading...
 
Back
Top