【问题标题】:Accessing the DataGridView Control on setting Property for Custom DataGridViewColumn在为自定义 DataGridViewColumn 设置属性时访问 DataGridView 控件
【发布时间】:2019-06-20 00:47:32
【问题描述】:

可以帮助我了解如何在自定义DataGridViewColumn 的属性setter 中找到DataGridView 控件。我下面的代码返回null。我想获得我的自定义列的父 DataGridView

public class CustomComboBoxColumn : DataGridViewComboBoxColumn
{
  public string SomeProperty
  {
    get { return _SomeProperty; }
    set
        {
          _SomeProperty = value;
          CustomDataGridView dgv = this.DataGridView;  //this one is returning null

        }
   }
}

【问题讨论】:

  • 您的 Datagridview id 是否称为“DataGridView”?这看起来就像您正在尝试将 dgv 返回设置为一种类型,除非那是对象 ID。因此,如果它是 id,那么它是否可能在某种模板或面板中?
  • 我编辑了我的问题。错误是 this.DataGridView;总是返回null。那么有没有办法获取自定义列的父datagridview呢?
  • 这个属性是什么时候设置的?该列是否已添加到 DGV 的 Columns 集合中?如果操作正确,该属性将返回它所属的 DGV。顺便说一句,使用var dgv = this.DataGridView;

标签: c# winforms datagridview


【解决方案1】:

DataGridView(在 WinForms 中 - 从 post 标签判断)是 DataGridViewColumnCollection Class 的属性 - 而不是 DataGridViewColumn Class 的属性所以既然你是从 DataGridViewColumn 派生类,您必须将此属性从自定义列集合滴流到添加到其中的每个自定义列,也许您可​​以通过实现 Column.Collection 自定义属性使集合本身在每个添加的列中可访问,并且每次将列添加到集合时都会对其进行初始化。然后你可以在你的 CustomComboBoxColumn 类中使用类似这样的东西: CustomDataGridView dgv = this.CustomComboBoxColumnCollection.DataGridView; .

或者,考虑在包含它的集合中的 CustomComboBoxColumn 类中执行您打算执行的操作。在集合中,您可以访问CustomComboBoxColumnCollection.DataGridView 属性,然后在将新列添加到集合中时设置所需的结果。

做你觉得更自然且需要更少编码的事情:)

【讨论】:

    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多