As others have already pointed out, if the header title etc is known at design time, turn off AutoGeneratedColumns and just set the title etc in the field definition instead of using auto generated columns. From your example it appears that the query is static and that the titles are known at design time so that is probably your best choice.
However [, although your question does not specify this requirement] - if the header text (and formatting etc) is not known at design time but will be determined at runtime and if you need to auto generate columns (using AutoGenerateColumns=
true") there are workarounds for that.
One way to do that is to create a new control class that inherits the gridview. You can then set header, formatting etc for the auto generated fields by overriding the gridview's "CreateAutoGeneratedColumn". Example:
//gridview with more formatting options
namespace GridViewCF
{
[ToolboxData("<{0}:GridViewCF runat=server></{0}:GridViewCF>")]
public class GridViewCF : GridView
{
//public Dictionary<string, UserReportField> _fieldProperties = null;
public GridViewCF()
{
}
public List<FieldProperties> FieldProperties
{
get
{
return (List<FieldProperties>)ViewState["FieldProperties"];
}
set
{
ViewState["FieldProperties"] = value;
}
}
protected override AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
{
AutoGeneratedField field = base.CreateAutoGeneratedColumn(fieldProperties);
StateBag sb = (StateBag)field.GetType()
.InvokeMember("ViewState",
BindingFlags.GetProperty |
BindingFlags.NonPublic |
BindingFlags.Instance,
null, field, new object[] {});
if (FieldProperties != null)
{
FieldProperties fps = FieldProperties.Where(fp => fp.Name == fieldProperties.Name).Single();
if (fps.FormatString != null && fps.FormatString != "")
{
//formatting
sb["DataFormatString"] = "{0:" + fps.FormatString + "}";
field.HtmlEncode = false;
}
//header caption
field.HeaderText = fps.HeaderText;
//alignment
field.ItemStyle.HorizontalAlign = fps.HorizontalAlign;
}
return field;
}
}
[Serializable()]
public class FieldProperties
{
public FieldProperties()
{ }
public FieldProperties(string name, string formatString, string headerText, HorizontalAlign horizontalAlign)
{
Name = name;
FormatString = formatString;
HeaderText = headerText;
HorizontalAlign = horizontalAlign;
}
public string Name { get; set; }
public string FormatString { get; set; }
public string HeaderText { get; set; }
public HorizontalAlign HorizontalAlign { get; set; }
}
}