是否可以遍历所有 HeaderRow Cells ?
目前,我正在这样做,这很烦人/乏味
gv.HeaderRow.Cells[0].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[1].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[2].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[3].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[4].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[5].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[6].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[7].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[8].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[9].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[10].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[11].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[12].BackColor = System.Drawing.Color.LightCyan;
感谢任何帮助。
更新
public ActionResult ExportToExcel()
{
List<DailySummary> lstDailySummary = db.DailySummaries.Include(x => x.codeAC).Include(x => x.codeC).Include(x => x.codeAcy).Include(x => x.ACInfo).ToList();
GridView gv = new GridView();
lstDailySummary = (List<DailySummary>)Session["Paging"];
List<ExportExcel> lstExportedExcel = new List<ExportExcel>();
foreach(var item in lstDailySummary)
{
ExportExcel export = new ExportExcel();
if (!string.IsNullOrWhiteSpace(item.Acy.ToString()))
{
export.Acy = item.codeAcy.location;
}
else
{
export.Acy = " ";
}
export.AC = item.ACInfo.RNumber;
export.ATime = item.ATime;
export.Assignment = item.codeAC.text;
export.County = item.codeC.county;
export.Flight = item.Flight;
export.FlightDay = item.FlightDay;
export.INumber = item.INumber;
export.Location = item.codeL.Location;
export.RINS = item.RINS;
export.Starts = item.Starts;
export.Summary = item.Summary;
export.Total = item.Total;
lstExportedExcel.Add(export);
gv.DataSource = lstExportedExcel.ToList();
gv.DataBind();
}
gv.ID = "DailySummaryExcel";
//protected void gv_PreRender(object sender, EventArgs e)
//{
// foreach (DataControlField dcf in gv.Columns)
// {
// if (dcf.HeaderText != string.Empty)
// dcf.HeaderStyle.BackColor = System.Drawing.Color.LightCyan;
// }
//}
gv.HeaderRow.Cells[0].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[1].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[2].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[3].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[4].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[5].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[6].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[7].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[8].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[9].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[10].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[11].BackColor = System.Drawing.Color.LightCyan;
gv.HeaderRow.Cells[12].BackColor = System.Drawing.Color.LightCyan;
//gv.HeaderRow.BackColor = System.Drawing.Color.LightCyan;
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=DailySummaryExport.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("Index");
}
答案 0 :(得分:0)
为什么要循环?在aspx中:
<HeaderStyle BackColor="LightCyan" />
或在后面的代码中:
gv.HeaderRow.BackColor = System.Drawing.Color.LightCyan;
由于您只需要进行样式更改,您可以在呈现之前点击GridView:
protected void gv_PreRender( object sender, EventArgs e ) {
foreach ( DataControlField dcf in gv.Columns ) {
if ( dcf.HeaderText != string.Empty )
dcf.HeaderStyle.BackColor = System.Drawing.Color.LightCyan;
}
}
在Gridview标记中添加函数作为事件处理程序:
<asp:GridView ID="gv" runat="server" OnPreRender="gv_PreRender" />
或以编程方式:
gv.PreRender += gv_PreRender ;
在设计器中查看GridView并选择控件。
选择后,打开/查看“属性”窗口。在顶部附近,在窗口的图标栏中,您应该看到Lightning图标。
如果选择此项,“属性”页面将切换到事件查看器。
这将列出与设计器中所选控件关联的所有事件。很容易看出正在处理哪些事件以及从这里向控件添加新事件