造型的GridView"标题"从C#类 [英] Styling a Gridview "Caption" from C# class

查看:187
本文介绍了造型的GridView"标题"从C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图风格ASP.Net的GridView的标题在C#文件。这里是我的方法,该方法返回一个风格的GridView:

I'm trying to style the caption of a ASP.Net GridView in a C# file. here is my method that returns a styled GridView:

private GridView setupGridView(string caption)
{
 var gview = new GridView()
 {
  BackColor = Color.White,
  BorderColor = Color.Gray,
  BorderStyle = BorderStyle.Solid,
  BorderWidth = new Unit(1, UnitType.Pixel),
  Caption = caption,
  ForeColor = Color.Black,
 };
 gview.HeaderStyle.BackColor = Color.Navy;
 gview.HeaderStyle.ForeColor = Color.White;
 gview.HeaderStyle.BorderColor = Color.DarkGray;
 gview.HeaderStyle.BorderWidth = new Unit(1, UnitType.Pixel);
 gview.HeaderStyle.BorderStyle = BorderStyle.Solid;
 gview.AlternatingRowStyle.BackColor = Color.LightGray;
 return gview;
}

默认情况下,标题是不是风格(这是在gridview的顶部只黑色文本)

By default the Caption is not styled (it's just black text on top of the gridview)

有谁知道我可以用白色文本样式的标题海军? (类似我曾经也许风格的标题行的方式吗?)

Does anyone know how I can style the Caption Navy with white text? (similar to the way I have styled the header row maybe?)

编辑:我以前用CSS这样做了,但我没有这样做,因为,这是产生GridView的在电子邮件发送程序的自由。没有aspx文件或皮肤...

I've done this before by using CSS, but I don't have the liberty of doing that as, this is a program that generates gridviews to send in an email. There is no aspx file or skin...

推荐答案

因此​​,与其他开发人员交谈之后,我们想出了这个。 (顺便说一句,我踢自己,因为它是如此简单)

So after talking with another developer, we came up with this. (BTW I'm kicking myself because it is so simple)

要写出GridView控件在电子邮件发送,我是使用像这样的的HTMLWriter:

To write out the gridview for sending in the email, I was using an htmlwriter like so:

var sbuilder = new StringBuilder();
var swriter = new StringWriter(sbuilder);
var htmlWriter = new HtmlTextWriter(swriter);
//...
myGridview.RenderControl(htmlwriter);
mymailMessage.Body = sbuilder.ToString();

我所要做的就是使用的HTMLWriter呈现控件之前添加的样式。

All I had to do was add the styles before using the htmlwriter to render the controls.

所以我加入这个创建的HTMLWriter后

So I added this after creating the htmlwriter

htmlWriter.Write("<style type=\"text/css\">caption { font-family: Arial, sans-serif; color: white; font-size: 14px; font-weight: bold; padding: 3px 100px 3px 7px; text-align: left; white-space: nowrap; text-align: center; background-color: navy;}</style>");

现在我当然会把这个字符串中的某个地方一个配置文件,这样我就不必重新编译该项目以改变电子邮件的风格,但是这是所有我需要做的。

Now of course I will put this string in a config file somewhere, so I don't have to recompile the project to change the style of the email, but this is all I needed to do.

瓦剌!所有字幕风格一样...

Wala! all captions are styled the same...

这篇关于造型的GridView&QUOT;标题&QUOT;从C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆