在 DataGridView 中使用自定义控件 [英] Use a custom control in DataGridView

查看:56
本文介绍了在 DataGridView 中使用自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的自定义控件,其中包含一个文本框和一个按钮.其他自定义控件从这个通用控件继承并指定具体类型,例如:

I have a generic custom control which contains a textbox and a button. Other custom controls inherit from this generic one and specify a concrete type, eg:

public class CustomerType
{
    public int Id {get; set;}
    public int Number {get; set;}
    public string Description {get; set;}
}

通用控件有一个 Value 属性,它取决于 Type 参数和一个 Text 属性,它根据类型可能会显示 CustomerType.DescriptionProductType.Name.

The generic control has a Value property which depends on the Type parameter and a Text property, which, depending on the type, might display CustomerType.Description or ProductType.Name.

这些控件用于搜索(例如客户类型、产品类型等).

These controls are used for search (eg. customer types, product types, etc).

这些控件放置在普通表单上时可以正常工作.现在我需要将这样的控件放在 DataGridView 列中,所以我尝试按照这个 MSDN 文章.

These controls work fine when placed on normal forms. Now I need to place such controls inside DataGridView columns, so I tried to follow this MSDN article.

目前,2 个派生类如下所示:

Currently, the 2 derived classes look like this:

public class CustomerTypeColumn : DataGridViewColumn
{
    public CustomerTypeColumn()
        : base(new CustomerTypeCell()) 
    { }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomerTypeCell)))
            {
                throw new InvalidCastException("Should be CustomerTypeCell.");
            }

            base.CellTemplate = value;
        }
    }
}

public class CustomerTypeCell : DataGridViewTextBoxCell
{
    public CustomerTypeCell() 
        : base() 
    { }

    public override Type ValueType
    {
        get
        {
            return typeof(CustomerType);
        }
    }
}

但是该列仍然显示一个文本框而不是我的自定义控件.我在这里缺少什么?此外,我将如何以及在哪里保存网格中单元格的 Value 属性?

But the column is still displaying a textbox instead of my custom control. What am I missing here? Furthermore, How and where am I going to save the Value property for the cell in grid?

推荐答案

您没有覆盖 CustomerTypeCellEditType.这就是为什么它仍然显示 DataGridViewTextBoxCell 的默认编辑控件,它是一个 TextBox.

You haven't overridden the EditTypeof CustomerTypeCell. Thatswhy its still showing the default editing control of DataGridViewTextBoxCell which is a TextBox.

您必须创建自定义编辑控件,就像在您引用的 MSDN 链接中一样,或者在您的自定义控件中实现 IDataGridViewEditingControl 控件.在 EditType 中返回自定义编辑控件的类型.

You have to create custom editing control just like in the MSDN link you have referenced or implement the IDataGridViewEditingControl control in your custom control. Return the type of the custom editing control in the EditType.

public class CustomerTypeCell : DataGridViewTextBoxCell
{
    public CustomerTypeCell() 
        : base() 
    { }

    public override Type ValueType
    {
        get
        {
            return typeof(CustomerType);
        }
    }

    public override Type EditType
    {
        get
        {                 
            return typeof(CustomEditingControl);
        }
    }       
}

单元格已经有一个值属性.将控件的 value 属性存储在其中.

The cell has already a value property. Store the control's value property in it.

这篇关于在 DataGridView 中使用自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆