简体   繁体   English

将自定义控件添加到DataGridViewCell

[英]Add Custom Control to DataGridViewCell

I create a custom control inherited from Windows.System.Forms.Controls. 我创建了一个继承自Windows.System.Forms.Controls的自定义控件。

This is my code of this control: 这是我控制的代码:

   public partial class MonthEventComponent : Control
    {
        private Color couleur;
        private Label labelEvenement;

        public MonthEventComponent(Color couleur_c, String labelEvenement_c )
        {
            InitializeComponent();
            this.couleur = couleur_c;
            this.labelEvenement.Text = labelEvenement_c;
            this.labelEvenement.ForeColor = couleur;
            this.labelEvenement.BackColor = Color.White;
            this.labelEvenement.TextAlign = ContentAlignment.MiddleLeft;
            this.labelEvenement.Dock = DockStyle.Fill;
            this.Controls.Add(labelEvenement);
        }

        public MonthEventComponent()
        {
            InitializeComponent();
            this.couleur = Color.Black;
            this.labelEvenement = new Label();
            this.labelEvenement.ForeColor = couleur;
            this.labelEvenement.BackColor = Color.White;
            this.labelEvenement.Text = "Evénement Initialiser";
            this.labelEvenement.TextAlign = ContentAlignment.MiddleLeft;
            this.labelEvenement.Dock = DockStyle.Fill;

            this.Controls.Add(labelEvenement);

        }


        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            MessageBox.Show("Click");
        }

    }

I would like to insert this control or multiple of this control on a DataGridViewCell but i don't know how to do this. 我想在DataGridViewCell上插入此控件或多个此控件,但我不知道如何执行此操作。

Thank you in advance for your answer, 提前感谢您的回答,

Best Regards, 最好的祝福,

PS: I'm french, i'm apologize for any can of language errors. PS:我是法国人,我为任何语言错误道歉。

I would assume you are using Winforms? 我会假设你使用Winforms?

Here is an MSDN tutorial on how to host a control in a Winforms DataGridViewCell . 是一个关于如何在Winforms DataGridViewCell托管控件的MSDN教程。

From the tutorial: 从教程:

The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. DataGridView控件提供多种列类型,使您的用户可以通过各种方式输入和编辑值。 If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing. 但是,如果这些列类型不满足您的数据输入需求,则可以使用托管您选择的控件的单元格创建自己的列类型。 To do this, you must define classes that derive from DataGridViewColumn and DataGridViewCell. 为此,您必须定义从DataGridViewColumn和DataGridViewCell派生的类。 You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface. 您还必须定义一个派生自Control的类并实现IDataGridViewEditingControl接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM