如何动态地隐藏字段一个DetailsView(场数始终为0)

如何动态地隐藏字段一个DetailsView(场数始终为0)

问题描述:

我使用的是 DetailsView控件来显示单行从细节数据表

I am using a DetailsView to show the details of a single row from a DataTable.

我的的知道在设计时的列名,所以我有 AutoGenerateRows = TRUE 的标记。

I do not know the column names at design time, so I have AutoGenerateRows = true in the markup.

DataView dv = myDataTable.AsDataView();
dv.RowFilter = string.Format("ResourceID = {0}", resourceId);

dvScoresBreakdown.DataSource = dv;
dvScoresBreakdown.DataBind();

有在数据视图我不希望 DetailsView控件来显示4列 - 主要是ID列。

There are about 4 columns in the DataView which I don't want the DetailsView to display - mainly ID columns.

我知道我应该访问数据视图的字段属性,并设置相关领域无形的:

I understand that I should access the Fields property of the DataView and set the relevant fields invisible:

dvScoresBreakdown.Fields[0].Visible = false;
dvScoresBreakdown.Fields[1].Visible = false;

但是, .Fields.Count 始终为零。所以,我得到一个索引越界异常。

However, the .Fields.Count is always zero. So I get an index out of bounds exception.

当我说始终为零,我的意思是零右后 .DataBind(),而且在 OnDataBinding OnDataBound 在preRender 事件。

When I say "always zero", I mean it's zero right after the .DataBind(), and also in the OnDataBinding, OnDataBound, and OnPreRender events.

不过,在DetailsView并呈现在页面上,并显示一切 - 在原来的数据视图中的所有列 - 这样的数据视图是绑定

But, the DetailsView does render on the page and show everything - all the columns in the original DataView - so the dataview is binding!

我在做什么错了?

我刚刚发现了,要做到这一点是右后 .DataBind()$ C删除行$ C>方法。

I've just found out, the way to do it is to remove rows right after the .DataBind() method.

dvScoresBreakdown.DataSource = dv;
dvScoresBreakdown.DataBind();

dvScoresBreakdown.Rows[0].Visible = false;
dvScoresBreakdown.Rows[1].Visible = false;

希望这可以帮助别人!

Hope this can help someone else!