C sharp - Problema con ingreso de datos

 
Vista:

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;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problema con ingreso de datos

Publicado por Fernando Frasco (3 intervenciones) el 07/11/2022 03:53:07
Se me olvido anexar que se esta trabajando en visual con lenguaje c#
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar