Quisiera agregar una nueva linea en un DataGridView
, dando doble click.
Pero no he logrado que agregue la nueva linea.
Para esto utilizo el evento:
private void Partes_MouseDoubleClick(object sender, MouseEventArgs e)
{ }
Partes
es el nombre de mi DataGridView
en el código.
public partial class Form1 : Form
{
DataTable datos = new DataTable();
public string datos = "";
public string descripcion = "";
public string material = "";
public string SN = "";
public Form1()
{
InitializeComponent();
crearDatatable();
this.Controls.Add(dataGridView1);
InitializeDataGridView();
}
private void InitializeDataGridView()
{
try
{
//DataGridView1.DataSource="";
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Add();
}
catch (InvalidCastException)
{
MessageBox.Show("Error al iniciarGrid");
}
}
public bool crearDatatable()
{
dataGridView1.Columns[0].HeaderText = "Datos";
dataGridView1.Columns[1].HeaderText = "Descripcion";
dataGridView1.Columns[2].HeaderText = "Material";
dataGridView1.Columns[3].HeaderText = "Numero de Serie";
return true;
}
//estos datos se mandan a un xml.
private void getData(int e)
{
datos = dataGridView1.Rows[e].Cells[0].Value.ToString();
descripcion = dataGridView1.Rows[e].Cells[1].ToString();
material = dataGridView1.Rows[e].Cells[2].ToString();
SN = dataGridView1.Rows[e].Cells[3].ToString();
return;
}
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeDataGridView();
}
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == Keys.Enter)
{
dataGridView1.Rows.Add();
dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells[0];
}
if (dataGridView1.CurrentCell.ColumnIndex == 4)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void Partes_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
}