473,289 Members | 1,840 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,289 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 8155
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.