【问题标题】:c# Hide a property in datagridview with datasource [duplicate]c#使用数据源隐藏datagridview中的属性[重复]
【发布时间】:2010-11-12 06:47:38
【问题描述】:

我认为必须有一个属性来隐藏数据网格视图中的公共属性。但我找不到它。

【问题讨论】:

标签: c# attributes datasource


【解决方案1】:

如果您自己添加列...不要添加您不想要的列。

如果您启用了AutoCreateColumns,那么:

  • 如果是基于类的模型,请将[Browsable(false)] 添加到您不想要的属性中
  • 或将列的.Visible 设置为false
  • 或者干脆删除之后不需要的列

【讨论】:

  • 另一个选项是在 AutoGeneratingColumn 处理程序中将 DataGridAutoGeneratingColumnEventArgs.Cancel 设置为 true。
  • 是的,BrowsableAttribute!这是我一整天都在寻找的东西。谢谢。
  • @Szybki IIRC,我发现它寻找的东西的唯一方法是查看反射器......从网格到PropertyDescriptor,再到PropertyInfo。不明显;p
  • 在哪里可以找到这个AutoCreateColumns
  • @Daniel 您是否正在使用 Web 控件?在撰写本文时,我认为这个问题指的是具有此属性的 winforms
【解决方案2】:
【解决方案3】:

根据您的问题,我想您不想在 datagridview 中显示某些“列”?如果是这样,请使用 Columns 属性添加和删除在用于附加到网格的数据源上找到的任何自动创建的列。

默认情况下,DataGridView 将为基础数据源对象上的所有公共属性创建列。所以,

public class MyClass
{
   private string _name;

   public string Name
   {
      get{ return _name; }
      set { _name = value; }
   }

   public string TestProperty
   {
      { get { return "Sample"; }
   }
}

...
[inside some form that contains your DataGridView class]

MyClass c = new MyClass();

// setting the data source will generate a column for "Name" and "TestProperty"
dataGridView1.DataSource = c;

// to remove specific columns from the DataGridView
// dataGridView1.Columns.Remove("TestProperty")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2011-11-23
    • 2019-08-16
    • 2022-06-30
    • 1970-01-01
    相关资源
    最近更新 更多