Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to learn how to make custom controls.

I am trying to make a dataGridView with a Treeview Column (Cell Editing control not how the data is displayed in the cell) based on this article Here.

But I can't tell my code how to take the treeview nodes that will be displayed in the editing control.

This is the code I typed so far:

namespace DGVTV
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class TreeviewColumn : DataGridViewColumn
    {
        
        public TreeviewColumn()
            : base(new TreeviewCell())
        {

        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [MergableProperty(false)]
        [Browsable(true)]
        public TreeNodeCollection Nodes { get; set; }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a TreeviewCell.
                if (value != null && !value.GetType().IsAssignableFrom(typeof(TreeviewCell)))
                {
                    throw new InvalidCastException("Must be a TreeviewCell");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class TreeviewCell : DataGridViewTextBoxCell
    {

        public TreeviewCell()
            : base()
        {
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            TreeviewEditingControl ctl = DataGridView.EditingControl as TreeviewEditingControl;
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing control that TreeviewCell uses.
                return typeof(TreeviewEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that TreeviewCell contains.
                return typeof(string);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                return null;
            }
        }
    }

    class TreeviewEditingControl : TreeView, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public TreeviewEditingControl()
        {
            
        }


        public object EditingControlFormattedValue { get; set; }
        

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return null;
        }


        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
        }


        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }


        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            return false;
        }


        public void PrepareEditingControlForEdit(bool selectAll)
        {

        }


        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }


        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }


        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }


        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

    }
}


Things I encounter so far:

1) When I enter value in the Nodes property for the TreeviewColumn the nodes are not saved.

2) I can't find a way to transfer the data to the EditingControl of the dataGridView as there is no direct call from the class 'TreeviewEditingControl' to the class 'TreeviewCell'

Can anyone help me please, as I always find custom controls are very hard to learn I tried many codes but without understanding the code properly I am just copy pasting and changing a few things that don't always give me what I want.

What I have tried:

The code mentioned above, I tried it with DateTimePicker as in this article.
Posted
Updated 13-Dec-20 22:11pm

You don't put a "treeview in a cell"; the best you can hope to do is link to a tree view (somewhere) based on the value in the cell. (like a BOM#).

The article discusses the "Date Picker"; which "lauches" a (.NET) calendar type control; it's not "in" the cell.
 
Share this answer
 
See: [^] for an article on a TreeListView here. There are lots of 3rd. party controls, like DotNetBar: (commercial) [^].

Using what the ToolBox provides: consider a SplitContainer with a TreeView in Panel1, and a DataGridView in Panel2 ... or a Panel with both controls.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900