user1676874
user1676874

Reputation: 905

How to change format of a GridView field from code behind?

I can show a GridView with the data I need from code behind like this:

SqlDataSource3.SelectCommand = "select * from table"

However I have a date field, which done like this gets displayed as 12/03/2012 12:00:00 a.m. for example. When done on ASP it can be fixed like this:

<asp:BoundField DataField="DATE" DataFormatString="{0:dd/MM/yy}" HeaderText="DATE" ReadOnly="True" SortExpression="DATE" />

However I'm using only code behind. If I try to use the BoundField for date I get 2 date columns, and if I dont add the date field to the select statement then I get no date at all.

So anyway I can do this from code behind?

Upvotes: 0

Views: 2947

Answers (2)

Amir
Amir

Reputation: 724

Based on this link you can use below code:

BoundField dateField = new BoundField();
dateField.HeaderText = "Date";
dateField.DataField = "date";
dateField.DataFormatString = "{0:dd/MM/yy}";
gridView.Columns.Add(dateField);

Upvotes: 0

A.B.Cade
A.B.Cade

Reputation: 16905

I'm not to familiar with asp.net, but you can always change the format in the sql query:

select to_char(<your_date_column>, 'dd/mm/yy') "DATE" from table

But aren't you supposed to do it in the IIS in Globalization - colture ?

Upvotes: 1

Related Questions