473,785 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a column to a GridView at Runtime and DataFormatStrin g

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.Data FormatString = <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)DSSCaseCol umn.DataControl Fields.BoundFie ld:

BoundField boundField = new BoundField();
boundField.Data Field = column.Database FieldName;
boundField.Head erText = column.HeaderTe xt;
SetFieldWidth(b oundField, column.Percenta geColumnWidth);
if (column.IsDate == true)
{
boundField.Html Encode = true;
boundField.Data FormatString =
Constants.GRIDV IEW_DATE_DISPLA Y_FORMAT.ToStri ng();
}
SelectGridViewC ases.Columns.Ad d(boundField);
break;

May 16 '06 #1
4 8178
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.Dat aFormatString = <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)DSSCaseCol umn.DataControl Fields.BoundFie ld:

BoundField boundField = new BoundField();
boundField.Dat aField = column.Database FieldName;
boundField.Hea derText = column.HeaderTe xt;
SetFieldWidth( boundField, column.Percenta geColumnWidth);
if (column.IsDate == true)
{
boundField.Html Encode = true;
boundField.Data FormatString =
Constants.GRID VIEW_DATE_DISPL AY_FORMAT.ToStr ing();
}
SelectGridView Cases.Columns.A dd(boundField);
break;

You want to set:

boundField.Html Encode = 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.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{

}
protected void Button1_Click(o bject sender, EventArgs e)
{
BoundField boundField = new BoundField();
boundField.Data Field = "BirthDate" ;
boundField.Head erText = "BirthDateHeade r";
boundField.Html Encode = false;
boundField.Data FormatString = "{0:d}";
GridView1.Colum ns.Add(boundFie ld);
}
}
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.Data Field = column.Database FieldName;
boundField.Head erText = column.HeaderTe xt;
SetFieldWidth(b oundField, column.Percenta geColumnWidth);
if (column.IsDate == true)
{
boundField.Html Encode = false;
boundField.Data FormatString = "{0:" +
Constants.GRIDV IEW_DATE_DISPLA Y_FORMAT.ToStri ng() + "}";
}
SelectGridViewC ases.Columns.Ad d(boundField);
break;

May 17 '06 #5

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

Similar topics

6
3841
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 first two columns are pretty much text column, but subsequent columns 1-12 are numerical. I'm trying to get the thousand separator to work. Any ideas?
3
1621
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" DataFormatString="{0:yyyy/MM/dd}" It still shows the date as "2005/05/03 12:45 AM" It worked in beta 2, don't knwo about July CTP, but August CTP is a problem v2.0.50727
1
2006
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 Details view does not work. Runtime v2.0.50727. <asp:BoundField DataField="Event_Date" DataFormatString="{0:#0.0}" It still shows the value as 23.04
2
16356
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 DataFormatString is set to "{0:d}" in order to display the date in short date format (without the time). But the DataFormatString doesn't seem to have any effect. The DateTime gets displayed with the time. I have another GridView that is bound...
0
1768
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
2089
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 14:23:28". I want to display it as "03/30/02". I've included the code below. Can anyone offer an explanation? <asp:SqlDataSource ID="DD_Issues" runat="server"
0
2439
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 a data view (which in turn is loaded from a manually setup DataTable for testing purposes). One of the columns in the grid is a checkbox that's tied to an event handler that'll change the .Visible property of a column with a text box. That text...
3
1938
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 code, the GridView displays two sets of columns. The first set looks exactly as I would expect. The second set, which should not be there anyway, uses the DataField as the header and does not apply the DataFormatString for the third column. What...
9
6323
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 address. It's like it's trying to find the address locally. I know it probable has something to do with the url formatting, but not sure what needs to be done. <asp:GridView ID="gvEmployeesBenefits" runat="server" DataSourceID="Benefits"...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.