简体   繁体   English

如何将背景色添加到GridView的最后一行

[英]how to add background color to last line of GridView

I am using iText to export GridView to pdf. 我正在使用iText将GridView导出为pdf。 I was able to add header color to my pdf. 我能够将标题颜色添加到我的pdf中。

I would like to add background color to the last (total) line/row of pdf in the Gridview? 我想在Gridview中的pdf的最后(总)行/行中添加背景色吗?

I am using iText library to export GridView to Pdf. 我正在使用iText库将GridView导出到Pdf。

Here is my pdf function : 这是我的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();

    }


}

I tried using: GridView1.Rows[GridView1.Rows.Count - 1] to get the last row of gridview . 我尝试使用: GridView1.Rows[GridView1.Rows.Count - 1]获取gridview的最后一行。 But, I did not seem to make my way around it, to add color to last row. 但是,我似乎并没有设法在最后一行添加颜色。 Any help much appreciated. 任何帮助,不胜感激。

pdf格式

You can simply do this: 您可以简单地做到这一点:

GridView1.Rows[GridView1.Rows.Count - 1].BackColor = Color.Green;

Or if the row is a FooterRow: 或者,如果该行是FooterRow:

GridView1.FooterRow.BackColor = Color.Red;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM