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
{
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)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
TreeviewEditingControl ctl = DataGridView.EditingControl as TreeviewEditingControl;
}
public override Type EditType
{
get
{
return typeof(TreeviewEditingControl);
}
}
public override Type ValueType
{
get
{
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.