Problema con ingreso de datos
Publicado por Fernando Frasco (3 intervenciones) el 07/11/2022 03:51:11
Buenas, tengo un pequeño problema al intentar ingresar datos a un dtagridview donde se importo un excel no me lo permite o si se ingresa pero en la fila[0] y no se agregan mas filas ya intente de distintas maneras y nada, use oledb connection para importar el excel, algunas cosas como abrir o cerrar la conexxion o ejecutar el query no me lo permite
este es el codigo:
DataView ImportarDatos(string nombreArchivo)
{
string conexion = string.Format("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = {0};Extended Properties = 'Excel 12.0'", nombreArchivo);
OleDbConnection conector = new OleDbConnection(conexion);
conector.Open();
OleDbCommand consulta = new OleDbCommand("select * from [Hoja2$]", conector);
OleDbDataAdapter adaptador = new OleDbDataAdapter
{
SelectCommand = consulta
};
DataSet ds = new DataSet();
adaptador.Fill(ds);
conector.Close();
return ds.Tables[0].DefaultView;
}
private void btnImportar_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Excel | * .xls;*.xlsx;",
Title = "Seleccionar Archivo",
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
dgvDatosPagos.DataSource = ImportarDatos(openFileDialog.FileName);
}
}
private void btnAgregar_Click(object sender, EventArgs e)
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Hoja2$ (A, B, C, D, E) VALUES ('"+dtFecha.Text+"','" +tbPaciente+"','" +tbMedico+"','" +tbServicio+"','" +tbPrecio+"')";
dgvDatosPagos.CurrentRow.Cells[0].Value = dtFecha.Text;
dgvDatosPagos.CurrentRow.Cells[1].Value = tbPaciente.Text;
dgvDatosPagos.CurrentRow.Cells[2].Value = tbMedico.Text;
dgvDatosPagos.CurrentRow.Cells[3].Value = tbServicio.Text;
dgvDatosPagos.CurrentRow.Cells[4].Value = tbPrecio.Text;
}
este es el codigo:
DataView ImportarDatos(string nombreArchivo)
{
string conexion = string.Format("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = {0};Extended Properties = 'Excel 12.0'", nombreArchivo);
OleDbConnection conector = new OleDbConnection(conexion);
conector.Open();
OleDbCommand consulta = new OleDbCommand("select * from [Hoja2$]", conector);
OleDbDataAdapter adaptador = new OleDbDataAdapter
{
SelectCommand = consulta
};
DataSet ds = new DataSet();
adaptador.Fill(ds);
conector.Close();
return ds.Tables[0].DefaultView;
}
private void btnImportar_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Excel | * .xls;*.xlsx;",
Title = "Seleccionar Archivo",
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
dgvDatosPagos.DataSource = ImportarDatos(openFileDialog.FileName);
}
}
private void btnAgregar_Click(object sender, EventArgs e)
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Hoja2$ (A, B, C, D, E) VALUES ('"+dtFecha.Text+"','" +tbPaciente+"','" +tbMedico+"','" +tbServicio+"','" +tbPrecio+"')";
dgvDatosPagos.CurrentRow.Cells[0].Value = dtFecha.Text;
dgvDatosPagos.CurrentRow.Cells[1].Value = tbPaciente.Text;
dgvDatosPagos.CurrentRow.Cells[2].Value = tbMedico.Text;
dgvDatosPagos.CurrentRow.Cells[3].Value = tbServicio.Text;
dgvDatosPagos.CurrentRow.Cells[4].Value = tbPrecio.Text;
}
Valora esta pregunta


0