改变从code-背后的GridView列属性 [英] Changing GridView column properties from code-behind

查看:120
本文介绍了改变从code-背后的GridView列属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在像这样的方法创建一个GridView:

I am creating a GridView in a method like so:

GridView gridView = new GridView();
gridView.DataSource = reportData.Tables[0];
gridView.DataBind();

我后来将其导出到Excel和它的伟大工程。列正在从我的源数据的自动生成。我想不过经过我和数据绑定我导出到Excel之前的一些列的DataFormatString属性更改。我似乎无法找到正确的属性更改。任何人都可以点我在正确的方向?

I am later exporting it to Excel and it works great. The columns are being auto-generated from my source data. I would like to change the DataFormatString property of some of the columns however after I databind and before I export to Excel. I can't seem to find the correct property to change. Can anybody point me in the right direction?

推荐答案

据<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx\">AutoGenerateColumns文档:

这个选项提供了一种方便的方法
  在要显示的数据的每场
  资源;但是,你必须的限制
  如何控制的自动
  显示生成的列字段或
  行为
的。

This option provides a convenient way to display every field in the data source; however, you have limited control of how an automatically generated column field is displayed or behaves.

请注意:自动
  生成绑定列字段
  添加到列集合

我厌倦了寻找这些 AutoGeneratedField s的没有运气。

我能想到的几个选项来实现这一(从最差到最好):

I tired looking for these AutoGeneratedFields with no luck.
I can think of several options to achieve that (from worst to best):


  1. 事件添加到网格(如的RowDataBound ),这会给你访问的行'细胞,但不是太方便。

  2. 不要使用 AutoGeneratedField 手动创建这些列,如:

  1. Add an event to the grid (like RowDataBound), this will give you access to the rows' cells, but isn't too convenient.
  2. Don't use AutoGeneratedField Create these columns manually, as in:

BoundField dateField = new BoundField();
dateField.HeaderText = "Date";
dateField.DataField = "date";
dateField.DataFormatString = "{0:MMMM, yyyy}";
gridView.Columns.Add(dateField);

此选项让你在BES职称控制。

This options gives you bes control over titles.

和,作为最后一个音符,你应该考虑使用API​​创建Excel文件。这并不容易,但HTML XLS文件与Excel 2007中较少的兼容 - 它显示一条警告消息,该文件的格式是扩展糟糕不相容的,而且,该文件刹车,如果它被打开并保存(可另存为虽然),使您的文件不太人性化。

And, as a last note, you should think about creating Excel files by using the API. It's not as easy, but HTML XLS files are less compatible with Excel 2007 - it displays a warning message that the format of the file is incompatible with the extension, and worse, the file brakes if it is opened and saves (can be Save As though), making your files less user-friendly.

这篇关于改变从code-背后的GridView列属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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