如何从C#中更改GridView中的列格式 [英] How to change the format of a column in GridView from c#

查看:162
本文介绍了如何从C#中更改GridView中的列格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,我想把它导出到Word。我想将一列的格式更改为日期时间格式(dd.MM.yyyy)

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

在* .aspx页面中,这很简单,看起来像下面的例子:

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

我想要从c#更改格式,该怎么做?

解决方案

如果要从CodeBehind
更改GridView列的格式添加 RowDataBound 到您的网格视图。



然后在 GridView_RowDataBound(object sender,GridViewRowEventArgs e)方法中,可以访问 e ,这将为您提供访问该行的各个单元格,您可以在其中指定格式化程序。



以下是使用 RowDataBound 事件
http://forums.asp.net/p/1589807/4025153.aspx


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

这篇关于如何从C#中更改GridView中的列格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆