如何在设计时在“编辑列”对话框窗体中设置标准DataGridViewColumns不可见

时间:2017-03-05 06:58:31

标签: c# .net winforms datagridview

我正在为自己的MyDataGridView控件开发自己的列。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;

namespace WindowsFormsApplication1
{
    public class MyDataGridView : System.Windows.Forms.DataGridView
    {
    }

    public class MyDataGridViewComboBoxColumn : System.Windows.Forms.DataGridViewComboBoxColumn
    {
    }

    public class MyDataGridViewTextBoxColumn : System.Windows.Forms.DataGridViewTextBoxColumn
    {
    }

    public class MyDataGridViewCheckBoxColumn : System.Windows.Forms.DataGridViewCheckBoxColumn
    {
    }

    public class MyDataGridViewButtonColumn : System.Windows.Forms.DataGridViewButtonColumn
    {
    }

    public class MyDataGridViewLinkColumn : System.Windows.Forms.DataGridViewLinkColumn
    {
    }

    public class MyDataGridViewImageColumn : System.Windows.Forms.DataGridViewImageColumn
    {
    }
}

我想知道是否可以在设计时隐藏编辑列对话框窗体中的System.Windows.Forms中定义的标准DataGridView列,以便我只能使用自己的列。< / p>

1 个答案:

答案 0 :(得分:1)

我出来了。

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MyDataGridView : System.Windows.Forms.DataGridView
    {
        public override ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                base.Site = value;

                if (Site != null)
                {
                    IDesignerHost host = (IDesignerHost)Site.GetService(typeof(IDesignerHost));
                    if (host != null)
                    {
                        host.RemoveService(typeof(ITypeDiscoveryService));
                        host.AddService(typeof(ITypeDiscoveryService), new TypeDiscoveryService());
                    }
                }
            }
        }

        static readonly Type[] columnTypes = new Type[] 
                { 
                    typeof(MyDataGridViewButtonColumn), 
                    typeof(MyDataGridViewCheckBoxColumn), 
                    typeof(MyDataGridViewLinkColumn), 
                    typeof(MyDataGridViewImageColumn), 
                    typeof(MyDataGridViewComboBoxColumn), 
                    typeof(MyDataGridViewTextBoxColumn) 
                };

        class TypeDiscoveryService : ITypeDiscoveryService
        {
            ICollection ITypeDiscoveryService.GetTypes(Type baseType, bool excludeGlobalTypes)
            {
                return columnTypes;
            }
        }

    }

    [System.Windows.Forms.DataGridViewColumnDesignTimeVisible(true)]
    public class MyDataGridViewComboBoxColumn : System.Windows.Forms.DataGridViewComboBoxColumn
    {
    }

    [System.Windows.Forms.DataGridViewColumnDesignTimeVisible(true)]
    public class MyDataGridViewTextBoxColumn : System.Windows.Forms.DataGridViewTextBoxColumn
    {
    }

    [System.Windows.Forms.DataGridViewColumnDesignTimeVisible(true)]
    public class MyDataGridViewCheckBoxColumn : System.Windows.Forms.DataGridViewCheckBoxColumn
    {
    }

    [System.Windows.Forms.DataGridViewColumnDesignTimeVisible(true)]
    public class MyDataGridViewButtonColumn : System.Windows.Forms.DataGridViewButtonColumn
    {
    }

    [System.Windows.Forms.DataGridViewColumnDesignTimeVisible(true)]
    public class MyDataGridViewLinkColumn : System.Windows.Forms.DataGridViewLinkColumn
    {
    }

    [System.Windows.Forms.DataGridViewColumnDesignTimeVisible(true)]
    public class MyDataGridViewImageColumn : System.Windows.Forms.DataGridViewImageColumn
    {
    }




}

下面的图片显示了代码的结果

enter image description here