How to set GridView DataFormatString programmatically forAutoGenerateColumns?

  • Thread starter Thread starter Paul
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top