473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Adding a column to a GridView at Runtime and DataFormatString

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;

May 16 '06 #1
4 8158
On 16 May 2006 05:18:31 -0700, "Eamonn" <ea************@hotmail.com>
wrote:
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
May 16 '06 #2
Thanks Peter. I have tried both true and false. Neither has any effect.

May 16 '06 #3
On 16 May 2006 09:00:24 -0700, "Eamonn" <ea************@hotmail.com>
wrote:
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
May 16 '06 #4
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;

May 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hutty | last post by:
I've looked around and have yet to find anything that would answer my question regarding formating a column in a datagrid. My grid looks like this as far as data" AMHQCON|51300.01|-3147 The...
3
by: Gerhard Pretorius | last post by:
Since installing the VS.NET August CTP the DataFormatString of the BoundField in the Gridview does not work. Runtime v2.0.50727. <asp:BoundField DataField="Event_Date"...
1
by: Gerhard Pretorius | last post by:
I am reposting this..hoping someone (from Microsoft) can confirm this at least as a problem: Since installing the VS.NET August CTP the DataFormatString of the BoundField in the Gridview and...
2
by: Rob Roberts | last post by:
I have a GridView that is bound to a collection of DateTimes (i.e. - Collection<DateTime>). I have a BoundField in the GridView, and its DataField is set to the DateTime's Date property, and the...
0
by: jobo | last post by:
Hey there, I'm having a problem getting sorting to work. Here's what the GridView looks like: "server" ID="updt1" Mode="Conditional">
2
by: pgonzo | last post by:
In my initial tests with the GridView control, I cannot understand why the DataFormatString attribute has no affect on the column formatting. The 'creation_date' field is being reported as "3/30/02...
0
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from...
3
by: Nathan Sokalski | last post by:
I have a GridView control with three columns, all BoundField columns. They all have a HeaderText and DataField property set, and the third one has a DataFormatString property as well. When I run my...
9
by: kimbred | last post by:
I have a gridview that has a hyperlink column. It gets the web address from a data source. This is all working as it should. The problem is when I click the web address, it doesn't go to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.