简体   繁体   English

不自动显示ASP.NET Web表格gridview中的列的数据注释?

[英]Data annotation for not autodisplay columns in asp.net web forms gridview?

I'm currently working in asp.net web forms 4.5. 我目前正在使用asp.net网络表单4.5。

I'm using gridview and gridview has a nice feature called autogenerate columns, and I'm trying to use it but there are some columns I don't want to show. 我正在使用gridview,并且gridview具有一个很好的功能,称为自动生成列,并且我正在尝试使用它,但是有些列我不想显示。

I know that I can set autogenerate column to false and call the columns for myself, but I was thinking if there is a feature such as data annotation on model, (I'm using database-first, by the way) and prevents the gridview from displaying. 我知道我可以将autogenerate列设置为false并为自己调用这些列,但是我在考虑是否有诸如模型上的数据注释之类的功能(顺便说一下,我使用数据库优先)并阻止了gridview从显示。

The closest post I found is data annotations hide property/field but I don't think this applies to my case. 我发现最接近的帖子是数据注释hide属性/字段,但我认为这不适用于我的情况。

Thank you in advance! 先感谢您!

In WebForms there is no simple tagging like in MVC to hide columns with data annotations. 在WebForms中,没有像MVC中那样简单的标记来隐藏带有数据注释的列。 If you want to use autogenerate columns you can hide column in a data bound event using index of the column: 如果要使用自动生成列,则可以使用列索引在数据绑定事件中隐藏列:

protected void gridView_DataBound(object sender, GridViewRowEventArgs e) 
{
    e.Row.Cells[index].Visible = false;
}

I would advise not to use auto generated columns, but to explicitly specify each column in the gridview. 我建议不要使用自动生成的列,而应在gridview中显式指定每个列。 This way, you don't get any unexpected behavior in your GUI if your data source changes in the future. 这样,如果将来数据源发生更改,您的GUI中就不会出现任何意外行为。

If you've made a model with data annotations and you've given it a name that won't display in your gridview make sure you're setting the ItemType on the Gridview and pointing it towards you model. 如果您创建了带有数据注释的模型,并且为其指定了不会在gridview中显示的名称,请确保您在Gridview上设置了ItemType并将其指向您的模型。 It will work just fine. 它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM