如何使用c#在gridview中设置字体颜色和大小?

时间:2014-07-08 08:54:56

标签: c# asp.net pdf gridview itextsharp

我使用以下代码

将临时DataGrid导出为PDF
        System.Data.DataTable dt = new System.Data.DataTable();
        CDbAccess db = new CDbAccess();
        IDbConnection conn = db.GetConnectionInterface();
        conn.Open();

        IDbCommand cmd = db.GetCommandInterface(str);

        IDbDataAdapter da = db.GetDataAdapterInterface(cmd);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();

        try
        {
            da.SelectCommand = cmd;
            da.Fill(ds);
            dt = ds.Tables[0];
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
            // da.Dispose();
            conn.Dispose();
        }

        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        GridView1.HeaderStyle.BackColor = System.Drawing.Color.DeepSkyBlue;

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=BugReport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();

        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();

输出看起来更糟,所以我需要对齐输出。

我只想将字体颜色和字体大小设置为特定列。

如何使用C#进行设置?

1 个答案:

答案 0 :(得分:1)

Columns属性引用网格视图设置中的列,如果您只想访问特定行中的目标列,则:

GridView1.Rows[0].Cells[0].ControlStyle.Font.Size = 40;