【问题标题】:DataGridView as a property typeDataGridView 作为属性类型
【发布时间】:2011-10-29 22:35:35
【问题描述】:

我有一段类似下面的代码。我正在使用 Microsoft Visual C# Express Edition。我的问题是我无法在设计时向 DataGridView 添加列,它是另一个类的成员。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassLibrary1
{
    public class Class1 : Panel
    {
        DataGridView mView;

        public Class1()
        {
        mView = new DataGridView();
        this.Controls.Add(mView);
        }

        public DataGridView View
        {
            get { return mView; }
            set { mView = value; }
        }
    }
}

当我单击三个虚线按钮在属性窗口中添加新列时,我在设计时遇到了 System.NullException。由于我的声誉,我无法发布屏幕截图。

感谢您的帮助!

【问题讨论】:

标签: c# datagridview customproperty


【解决方案1】:

我只是更深入地研究了您的问题。不幸的是,我能够重现所描述的问题。在用户控件中对 DataGridView 进行一些研究后,我翻阅了以下 post

似乎对于继承的 DataGridView 没有视觉支持 - 而且 - 也不适用于使用 DataGridView 的 userControls。你也可以看看这个article

我尝试使用以下继承的 DataGridView 类 - 但都没有成功。很抱歉没有合适的解决方案 - 但我希望这对您有用。

/// tried this attribute - did not work
/// [Designer(typeof (System.Windows.Forms.Design.ControlDesigner))]

/// this did not work either
[Editor("System.Windows.Forms.Design.DataGridViewComponentEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(ComponentEditor))]
[Designer("System.Windows.Forms.Design.DataGridViewDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class ucInheritedDataGridView : DataGridView { }

【讨论】:

  • 实际上,我什至尝试了一个自定义数据网格对象,它继承了带有自定义列集合的 System.Windows.Forms.DataGridView。但它也没有奏效。第二个链接是我猜的答案。看来我现在必须编写自己的 DataGridView 了!谢谢..
猜你喜欢
  • 2016-05-13
  • 2012-09-14
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
相关资源
最近更新 更多