How to change the format of a column in GridView from c #

advertisements

I have a GridView and I want to export it to Word. I want to change the format of one column to datetime format ("dd.MM.yyyy")

var grid = new GridView { DataSource = dt};
grid.DataBind();

In *.aspx page this is very easy, it looks like in the example below:

<asp:boundfield datafield="My_Date_Column"
                dataformatstring="{0:dd.MM.yyyy}"
                htmlencode="false" />

I want change format from c#, how to do this?


If you want to alter the format of a column of a GridView from CodeBehind Add a RowDataBound to your grid view.

Then in the GridView_RowDataBound(object sender, GridViewRowEventArgs e) method, you'll be able to access e which will provide you with access to the individual cells of that row where you can specify a formatter.

Here's an example of using the RowDataBound event http://forums.asp.net/p/1589807/4025153.aspx