Gridview "Caption" styling from C # class

I am trying to create an ASP.NET GridView header in a C # file. here is my method that returns a stylized 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;
}

By default, Caption has no style (it's just black text on top of the gridview)

Does anyone know how I can style Caption Navy with white text? (similar to how, for example, I wrote a title bar?)

EDIT: I did this before using CSS, but I don’t have that option since it is a program that generates gridviews for sending via email. No aspx or skin file ...

+3
source share
2 answers

, , . (, , )

gridview , htmlwriter, :

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

, , htmlwriter .

, 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>");

, , -, , , , .

! ...

+3

gview.HeaderRow gview.HeaderStyle, . :

gview.HeaderRow.ForeColor = Drawing.Color.White

- (tr), html- .

+1

Source: https://habr.com/ru/post/1767944/


All Articles