将自定义控件添加到 DataGridViewCell [英] Add Custom Control to DataGridViewCell

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

问题描述

我创建了一个继承自 Windows.System.Forms.Controls 的自定义控件.

I create a custom control inherited from 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");
        }

    }

我想在 DataGridViewCell 上插入此控件或多个此控件,但我不知道如何执行此操作.

I would like to insert this control or multiple of this control on a DataGridViewCell but i don't know how to do this.

提前感谢您的回答,

此致,

PS:我是法国人,对于任何可能的语言错误,我深表歉意.

PS: I'm french, i'm apologize for any can of language errors.

推荐答案

我假设您使用的是 Winforms?

I would assume you are using Winforms?

这里是关于如何在 Winforms DataGridViewCell 中托管一个控件.

Here is an MSDN tutorial on how to host a control in a Winforms DataGridViewCell.

来自教程:

DataGridView 控件提供几种列类型,使您的用户输入和编辑值多种方式.如果这些列类型不满足您的数据输入需求,但是,您可以创建自己的带有承载单元格的列类型您选择的控件.去做这个,你必须定义派生类从 DataGridViewColumn 和数据网格视图单元格.您还必须定义派生自 Control 和实施IDataGridViewEditingControl 接口.

The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. 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. You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface.

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

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