. NET component Control instance programming series--3.datagridview column headers editable components

Source: Internet
Author: User

In the previous article introduced the use of a label control to simulate the link of a Web page component, the principle of implementation is simply to handle the Label control event. The DataGridView column headings that are described in this article can be edited to add more skill to the DataGridView control's events.

Let's begin by describing the effect this example will achieve. The DataGridView control in WinForm can only edit cells, but sometimes you need to edit the column headings, which is to customize the column headings. This component is the implementation of column header editing function, double-click the column headings can be edited, supporting the keyboard around the key mobile editing cell. Edit the effect as shown below. (Note: Double-click the column headers to perform a sort operation on some data sources, and if you want to avoid them, you can modify them to start editing by right-clicking the menu option.) )

It describes what you need to achieve, but DataGridView column headings are not available for editing, so how do you implement editing? This uses a RichTextBox control to simulate the edit state, overwriting the RichTextBox control over the column heading that you want to edit, and it looks like editing the column headings. This example is a bit more complicated than the previous one, not just a few simple events. The following is an introduction to the implementation process.

First create a new project, select the project type as the class library, enter the project name Datagridviewcolumnheadereditor, and then add the component Datagridviewcolumnheadereditor. The specific steps in the previous article has been introduced, will not elaborate.

As with the components described in the previous article, you must first specify an action target for the component. To operate here is DataGridView, so add a DataGridView type of property, add a property to indicate whether to allow editing, the code is as follows: The above mentioned the use of a RichTextBox control to simulate the editing effect, Then you need to add a RichTextBox control here. Switch to the Design view of the component and drag a RichTextBox control from the Toolbox into the assembly. Sets the related properties of the RichTextBox control, setting the multiline, TabStop, and visible to false.

The edit-enabled action is to double-click the column heading, and you need to double-click the event for the column header of the DataGridView control to process it. The previous article describes the story behind the form, which binds the event handler by setting the property, and also refers to the implementation in another way, which is the ISupportInitialize interface. This example uses this method to bind the control's events to the corresponding event handlers.

private DataGridView m_TargetControl = null;
        /// <summary>
        /// 要编辑的目标 DataGridView 控件
        /// </summary>
        [Description("要编辑的目标 DataGridView 控件。")]
        public DataGridView TargetControl
        {
            get { return m_TargetControl; }
            set { m_TargetControl = value; }
        }

        private bool m_EnableEdit = true;
        /// <summary>
        /// 是否允许编辑
        /// </summary>
        [Description("是否允许编辑。"), DefaultValue(true)]
        public bool EnableEdit
        {
            get { return m_EnableEdit; }
            set { m_EnableEdit = value; }
        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.