如何将背景颜色添加到GridView的最后一行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将背景颜色添加到GridView的最后一行相关的知识,希望对你有一定的参考价值。
我正在使用iText将GridView导出为pdf。我能够为我的pdf添加标题颜色。
我想在Gridview中为pdf的最后一行(总)添加背景颜色?
我正在使用iText库将GridView导出到Pdf。
这是我的pdf函数:
public void ExpToPdf_Click(object sender, EventArgs e)
{
gethtmlGridView();
}
private void getHTMLGridView()
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times16 = new iTextSharp.text.Font(bfTimes, 16, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
Paragraph p1 = new Paragraph("Texas Tech University", times16);
p1.Alignment = 1;
iTextSharp.text.Font times14 = new iTextSharp.text.Font(bfTimes, 14, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
Paragraph p2 = new Paragraph("Department of Institutional Research", times14);
Paragraph p3 = new Paragraph(caption.Text.ToUpper(), times14);
p2.Alignment = 1;
p3.Alignment = 1;
//iTextSharp.text.Font times10 = new iTextSharp.text.Font(bfTimes, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
//Paragraph p4 = new Paragraph("(Uncertified Data)", times10);
//p4.Alignment = 1;
Paragraph p5 = new Paragraph(" ");
iTextSharp.text.Font times8 = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
iTextSharp.text.Font times11 = new iTextSharp.text.Font(bfTimes, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.WHITE);
iTextSharp.text.Color BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#CC0000"));
PdfPTable HeaderTable = new PdfPTable(4);
//actual width of table in points
HeaderTable.TotalWidth = 600f;
//fix the absolute width of the table
HeaderTable.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
float[] Headerwidths = new float[] { 1f, 1f, 1f, 1f};
HeaderTable.SetWidths(Headerwidths);
HeaderTable.HorizontalAlignment = 1;
//leave a gap before and after the table
//HeaderTable.SpacingBefore = 30f;
//HeaderTable.SpacingAfter = 30f;
foreach (TableCell cell in MyGridView.HeaderRow.Cells)
{
PdfPCell pdfCell = new PdfPCell(new Phrase(cell.Text, times11));
// Set the PDF cell backgroundcolor to GridView header row BackgroundColor color
pdfCell.BackgroundColor = BackgroundColor;
pdfCell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
// Add the cell to PDF table
HeaderTable.AddCell(pdfCell);
}
PdfPTable table = new PdfPTable(4);
//actual width of table in points
table.TotalWidth = 600f;
//fix the absolute width of the table
table.LockedWidth = true;
//relative col widths in proportions
float[] widths = new float[] { 1f, 1f, 1f, 1f};
table.SetWidths(widths);
table.HorizontalAlignment = 1;
//leave a gap before and after the table
//table.SpacingBefore = 30f;
//table.SpacingAfter = 30f;
string connect = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnStringExtracts_IRDW"].ConnectionString;
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connect))
{
string query = "dbo.sp_FB_APPADM_IRDW";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@reportType", reportType);
cmd.Parameters.AddWithValue("@term_Code", ENRDropDownList.SelectedValue);
try
{
int i;
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
// Fill the DataSet.
adapter.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
for (i = 0; i < 2; i++)
{
PdfPCell pdfCell4 = new PdfPCell(new Phrase(row[i].ToString(), times8));
pdfCell4.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
// Add the cell to PDF table
table.AddCell(pdfCell4);
}
for (i = 2; i < ds.Tables[0].Columns.Count; i++)
{
PdfPCell pdfCell4 = new PdfPCell(new Phrase(row[i].ToString(), times8));
pdfCell4.HorizontalAlignment = 2; //0=Left, 1=Centre, 2=Right
// Add the cell to PDF table
table.AddCell(pdfCell4);
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
//Create the PDF Document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
//open the stream
pdfDoc.Open();
//add the table to the document
pdfDoc.Add(p1);
pdfDoc.Add(p2);
pdfDoc.Add(p3);
pdfDoc.Add(p5);
pdfDoc.Add(HeaderTable);
pdfDoc.Add(table);
//close the document stream
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" + "filename=Enrollment_Major_Classification.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
我尝试使用:GridView1.Rows[GridView1.Rows.Count - 1]
获得最后一排gridview
。但是,我似乎没有绕过它,为最后一行添加颜色。任何帮助非常感谢。
答案
你可以这样做:
GridView1.Rows[GridView1.Rows.Count - 1].BackColor = Color.Green;
或者如果行是FooterRow:
GridView1.FooterRow.BackColor = Color.Red;
以上是关于如何将背景颜色添加到GridView的最后一行的主要内容,如果未能解决你的问题,请参考以下文章
dev 控件,在gridcontrol中如何实现对满足条件的某一行进行涂色(改变背景颜色即可)