How to set GridView DataFormatString programmatically forAutoGenerateColumns?

P

Paul

Hello All,

Does anyone know how to set the DataFormatString of a column
programmatically. It tried to do it in the Gridview PrePaint method
and just about every other event I could think of with code like this:

((BoundField)grdDrillDownData.Columns[0]).DataFormatString = "{0:d-MMM-
yyyy}"

It didn't work because as I discovered, the AutoGenerateColumns are
not part of the Columns Collection. How then can one set this
programatically. I want to be able to format an autogenerated colum if
the data is a DateTime. I have to be able to do it programmatically
because I don't know what the data will be until runtime.

Thanks,

Paul
 
G

Guest

Hello All,

Does anyone know how to set the DataFormatString of a column
programmatically. It tried to do it in the Gridview PrePaint method
and just about every other event I could think of with code like this:

((BoundField)grdDrillDownData.Columns[0]).DataFormatString = "{0:d-MMM-
yyyy}"

It didn't work because as I discovered, the AutoGenerateColumns are
not part of the Columns Collection. How then can one set this
programatically. I want to be able to format an autogenerated colum if
the data is a DateTime. I have to be able to do it programmatically
because I don't know what the data will be until runtime.

Thanks,

Paul

According to MSDN

"Automatically generated bound column fields are not added to the
Columns collection. Instead of letting the GridView control
automatically generate the column fields, you can manually define the
column fields by setting the AutoGenerateColumns property to false and
then creating a custom Columns collection."

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx

I would recommend you to follow it and generate your columns yourself
from the DataSource. Otherwise, you would need to override
CreateAutoGeneratedColumns(PagedDataSource dataSource) and/or
CreateAutoGeneratedColumn(AutoGeneratedFieldProperties
fieldProperties) methods to provide AutoGeneratedField with
DataFormatString set as needed.

http://bytes.com/topic/asp-net/answ...ormatstring-code-gridview-autogeneratecolumns
 
P

Paul

Thanks for your help. That brings me closer to a solution. I am still
stuck trying to figure out how to create a custom columns collection
based on the data source.

All I was able to figure out so far was to do something like this in
the Page_Load event.

BoundField b = new BoundField();
b.DataField = "CallDate";
b.HeaderText = "Call Date";

grdDrillDownData.Columns.Add(b);

The trouble is that I need to know what kind of data is in the column
so that I know how to set the DataFormatString. How do I get the data
type from the source?

All help is appreciated.

Thanks,

Paul
 
G

Guest

Thanks for your help. That brings me closer to a solution. I am still
stuck trying to figure out how to create a custom columns collection
based on the data source.

All I was able to figure out so far was to do something like this in
the Page_Load event.

        BoundField b = new BoundField();
        b.DataField = "CallDate";
        b.HeaderText = "Call Date";

        grdDrillDownData.Columns.Add(b);

The trouble is that I need to know what kind of data is in the column
so that I know how to set the DataFormatString. How do I get the data
type from the source?

All help is appreciated.

Thanks,

Paul

Use GetFieldType Method for SqlDataReader

SqlClient.SqlDataReader dr;
if (dr.GetFieldType(0).Name == "DateTime")
....

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader..getfieldtype.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top