I have a datagridview that I fill by a query, the query returns values with alias, itm and I rename it as a product when exporting it, but when filling my excel it grabs the aliases of the query instead of the names of the datagridview column. a way to rename them when exporting?
code:
private void btnExportExcel_Click(object sender, EventArgs e)
{
ExportarExcel(dataGridView1);
}
public void ExportarExcel(DataGridView tabla)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
int IndiceColumna = 0;
foreach (DataGridViewColumn col in tabla.Columns) //Columnas
{
IndiceColumna++;
excel.Cells[1, IndiceColumna] = col.Name;
}
int IndiceFila = 0;
foreach (DataGridViewRow row in tabla.Rows) //Filas
{
IndiceFila++;
IndiceColumna = 0;
foreach (DataGridViewColumn col in tabla.Columns)
{
IndiceColumna++;
excel.Cells[IndiceFila + 1, IndiceColumna] = row.Cells[col.Name].Value;
}
}
excel.Visible =true;
}
How I rename my query data to fill in the datagridview
if (Datos.Rows.Count > 0)
{
dataGridView1.DataSource = Datos;
dataGridView1.Columns[0].Width = 80;
dataGridView1.Columns[0].HeaderText = "Localidad";
dataGridView1.Columns[1].Width = 120;
dataGridView1.Columns[1].HeaderText = "Producto";
dataGridView1.Columns[2].Width = 80;
dataGridView1.Columns[2].HeaderText = "Cantidad Cargada";