public static void CargarGrid <T>(ref Anthem.GridView Grid, List <T> Lista, string[] DataKey) { if (DataKey.Length != 0) { Grid.DataKeyNames = DataKey; } if (Lista.Count() != 0) { Grid.DataSource = Lista; Grid.DataBind(); Grid.UseAccessibleHeader = true; Grid.HeaderRow.TableSection = TableRowSection.TableHeader; } else { Grid.DataSource = null; Grid.EmptyDataRowStyle.CssClass = "GridHeader"; Grid.EmptyDataText = "No hay Registros para mostrar"; Grid.DataBind(); } }
//EJEMPLO DE LLAMADO // Excel.ExportarGrid<AuditoriaInfo>(gvBusqueda, NegAuditoria.ListaAuditoriaPDF, "Auditoria","Reporte de Auditoria", new int[] { 1, 3 }); //Agregar Método //public override void VerifyRenderingInServerForm(Control control){ } //Agregar en el Encabezado del Código HTML // EnableEventValidation="false" public static string ExportarGrid <T>(Anthem.GridView Grid, List <T> Lista, string NombreArchivo, string TituloReporte, int[] ColumnasAEliminar = null) { try { string Codigo = ""; StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); if (ColumnasAEliminar != null) { foreach (var Columna in ColumnasAEliminar) { if (Columna <= Grid.Columns.Count && Columna >= 1) { Grid.Columns[Columna - 1].Visible = false; } } } Grid.DataBind(); Grid.AllowPaging = false; Grid.DataSource = Lista; Grid.DataBind(); Grid.EnableViewState = false; Grid.RenderControl(htw); Codigo = Codigo + "<p>" + TituloReporte + "</p> <br />"; Codigo = Codigo + sb.ToString(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + NombreArchivo + ".xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = Encoding.Default; HttpContext.Current.Response.Write(Codigo); HttpContext.Current.Response.End(); Grid.AllowPaging = true; if (ColumnasAEliminar != null) { foreach (var Columna in ColumnasAEliminar) { if (Columna <= Grid.Columns.Count && Columna >= 1) { Grid.Columns[Columna - 1].Visible = true; } } } Grid.DataSource = Lista; Grid.DataBind(); return(""); } catch (Exception ex) { return(ex.Message); } }