Adding a column to a GridView at Runtime and DataFormatString

E

Eamonn

Hi

I am running Microsoft Visual Studio 2005 Team Edition Version
8.0.50727.42. I am adding columns to a GridView at runtime. When I add
a BoundField that will contain DateTime data I want to format the date
so I use:
BoundField.DataFormatString = <myDateFormat>

However it just displays the date formatting string in the GridView and
not the formatted date. I tried setting the HtmlEncode value for the
column to both true and false, but it makes no difference. A snippet of
my code is below. Any help would be appreciated.

Thanks

Eamonn

//BoundField
case (int)DSSCaseColumn.DataControlFields.BoundField:

BoundField boundField = new BoundField();
boundField.DataField = column.DatabaseFieldName;
boundField.HeaderText = column.HeaderText;
SetFieldWidth(boundField, column.PercentageColumnWidth);
if (column.IsDate == true)
{
boundField.HtmlEncode = true;
boundField.DataFormatString =
Constants.GRIDVIEW_DATE_DISPLAY_FORMAT.ToString();
}
SelectGridViewCases.Columns.Add(boundField);
break;
 
P

PeterKellner

Hi

I am running Microsoft Visual Studio 2005 Team Edition Version
8.0.50727.42. I am adding columns to a GridView at runtime. When I add
a BoundField that will contain DateTime data I want to format the date
so I use:
BoundField.DataFormatString = <myDateFormat>

However it just displays the date formatting string in the GridView and
not the formatted date. I tried setting the HtmlEncode value for the
column to both true and false, but it makes no difference. A snippet of
my code is below. Any help would be appreciated.

Thanks

Eamonn

//BoundField
case (int)DSSCaseColumn.DataControlFields.BoundField:

BoundField boundField = new BoundField();
boundField.DataField = column.DatabaseFieldName;
boundField.HeaderText = column.HeaderText;
SetFieldWidth(boundField, column.PercentageColumnWidth);
if (column.IsDate == true)
{
boundField.HtmlEncode = true;
boundField.DataFormatString =
Constants.GRIDVIEW_DATE_DISPLAY_FORMAT.ToString();
}
SelectGridViewCases.Columns.Add(boundField);
break;


You want to set:

boundField.HtmlEncode = false;

(not true)

Peter Kellner
http://peterkellner.net
Peter Kellner
http://peterkellner.net
 
P

PeterKellner

Thanks Peter. I have tried both true and false. Neither has any effect.

Very strange. Here is a small example I put together that works as I
describe. Give it a try and see what happens. (BirthDate is of
course a data column in my data object.

public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
BoundField boundField = new BoundField();
boundField.DataField = "BirthDate";
boundField.HeaderText = "BirthDateHeader";
boundField.HtmlEncode = false;
boundField.DataFormatString = "{0:d}";
GridView1.Columns.Add(boundField);
}
}
Peter Kellner
http://peterkellner.net
 
E

Eamonn

Hi Peter

Thanks to your example I spotted the issue. I needed to add the curly
braces { and } to the formatting string along with the ordinal
parameter. The following code now works fine:

BoundField boundField = new BoundField();
boundField.DataField = column.DatabaseFieldName;
boundField.HeaderText = column.HeaderText;
SetFieldWidth(boundField, column.PercentageColumnWidth);
if (column.IsDate == true)
{
boundField.HtmlEncode = false;
boundField.DataFormatString = "{0:" +
Constants.GRIDVIEW_DATE_DISPLAY_FORMAT.ToString() + "}";
}
SelectGridViewCases.Columns.Add(boundField);
break;
 
Top