如何动态地隐藏字段一个DetailsView(场数始终为0) [英] How to dynamically hide fields in a DetailsView (fields count is always 0)

查看:234
本文介绍了如何动态地隐藏字段一个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()方法。

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!

这篇关于如何动态地隐藏字段一个DetailsView(场数始终为0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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