程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#自界說DataGridViewColumn顯示TreeView

C#自界說DataGridViewColumn顯示TreeView

編輯:C#入門知識

C#自界說DataGridViewColumn顯示TreeView。本站提示廣大學習愛好者:(C#自界說DataGridViewColumn顯示TreeView)文章只能為提供參考,不一定能成為您想要的結果。以下是C#自界說DataGridViewColumn顯示TreeView正文


我們可以自界說DataGridView的DataGridViewColumn來完成自界說的列,上面引見一下若何經由過程擴大DataGridViewColumn來完成一個TreeViewColumn

1.TreeViewColumn類

 TreeViewColumn繼續自DataGridViewColumn,為了靜態給TreeViewColumn傳入一個TreeView,這裡裸露出一個公共屬性_root,可以綁定一個初始化的TreeView. 別的須要重寫DataGridCell類型的CellTemplate,這裡返還一個TreeViewCell(須要自界說) 

 /// <summary>
  /// Host TreeView In DataGridView Cell
  /// </summary>
  public class TreeViewColumn : DataGridViewColumn
  {
   public TreeViewColumn()
    : base(new TreeViewCell())
   {
   }
   [Description("Set TreeView Root in DataGridView Cell"), Category("TreeView")]
   public TreeView _root
   {
    get{return Roots.tree;}
    set{Roots.tree=value;}
   }
   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;
    }
   }
  } 

2.TreeViewCell類

    下面TreeViewColumn重寫了CellTemplate,前往的就是自界說的TreeViewCell,這裡就是詳細完成其邏輯。普通來講選擇樹控件的節點後,前往的是一個文本信息,是文本類型,可以繼續DataGridViewTextBoxCell,偏重寫InitializeEditingControl來停止自界說的DataGridView.EditingControl (編纂控件)。

 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;
    // Use the default row value when Value property is null.
    if (this.Value == null)
    {
     ctl.SelectedNode =new TreeNode( this.DefaultNewRowValue.ToString());
    }
    else
    {
     ctl.SelectedNode = new TreeNode(this.Value.ToString());
    }
   }
   public override Type EditType
   {
    get
    {
     // Return the type of the editing control that CalendarCell uses.
     return typeof(TreeViewEditingControl);
    }
   }
   public override Type ValueType
   {
    get
    {
     // Return the type of the value that CalendarCell contains.
     return typeof(String);
    }
   }
   public override object DefaultNewRowValue
   {
    get
    {
     // Use the current date and time as the default value.
     return "";
    }
   }
  } 

3.TreeViewEditingControl類

  TreeViewEditingControl為編纂控件,當用戶編纂TreeViewCell時,顯示的為樹編纂控件,須要繼續TreeView,同時完成IDataGridViewEditingControl接口,完成以下辦法:

 public class TreeViewEditingControl : TreeView, IDataGridViewEditingControl
  {
   DataGridView dataGridView;
   private bool valueChanged = false;
   int rowIndex;
   public TreeViewEditingControl()
   {
    try
    {
     //必需加Roots.tree.Nodes[].Clone() 不然報錯 不克不及在多處增加或拔出項,必需起首將其從以後地位移除或將其克隆
     this.Nodes.Add(Roots.tree.Nodes[].Clone() as TreeNode);
     this.SelectedNode = this.Nodes[];
    }
    catch (Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
   }
   // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
   // property.
   public object EditingControlFormattedValue
   {
    get
    {
     return this.SelectedNode.Text;
    }
    set
    {
     if (value is String)
     {
      try
      {
       // This will throw an exception of the string is 
       // null, empty, or not in the format of a date.
       this.SelectedNode = new TreeNode((String)value);
      }
      catch
      {
       // In the case of an exception, just use the 
       // default value so we're not left with a null
       // value.
       this.SelectedNode = new TreeNode("");
      }
     }
    }
   }
   // Implements the 
   // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
   public object GetEditingControlFormattedValue(
    DataGridViewDataErrorContexts context)
   {
    return EditingControlFormattedValue;
   }
   // Implements the 
   // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
   public void ApplyCellStyleToEditingControl(
    DataGridViewCellStyle dataGridViewCellStyle)
   {
    this.Font = dataGridViewCellStyle.Font;
    this.ForeColor = dataGridViewCellStyle.ForeColor;
    this.BackColor = dataGridViewCellStyle.BackColor;
   }
   // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
   // property.
   public int EditingControlRowIndex
   {
    get
    {
     return rowIndex;
    }
    set
    {
     rowIndex = value;
    }
   }
   // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
   // method.
   public bool EditingControlWantsInputKey(
    Keys key, bool dataGridViewWantsInputKey)
   {
    // Let the TreeViewPicker handle the keys listed.
    switch (key & Keys.KeyCode)
    {
     case Keys.Left:
     case Keys.Up:
     case Keys.Down:
     case Keys.Right:
     case Keys.Home:
     case Keys.End:
     case Keys.PageDown:
     case Keys.PageUp:
      return true;
     default:
      return !dataGridViewWantsInputKey;
    }
   }
   // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
   // method.
   public void PrepareEditingControlForEdit(bool selectAll)
   {
    // No preparation needs to be done.
   }
   // Implements the IDataGridViewEditingControl
   // .RepositionEditingControlOnValueChange property.
   public bool RepositionEditingControlOnValueChange
   {
    get
    {
     return false;
    }
   }
   // Implements the IDataGridViewEditingControl
   // .EditingControlDataGridView property.
   public DataGridView EditingControlDataGridView
   {
    get
    {
     return dataGridView;
    }
    set
    {
     dataGridView = value;
    }
   }
   // Implements the IDataGridViewEditingControl
   // .EditingControlValueChanged property.
   public bool EditingControlValueChanged
   {
    get
    {
     return valueChanged;
    }
    set
    {
     valueChanged = value;
    }
   }
   // Implements the IDataGridViewEditingControl
   // .EditingPanelCursor property.
   public Cursor EditingPanelCursor
   {
    get
    {
     return base.Cursor;
    }
   }
   protected override void OnAfterExpand(TreeViewEventArgs e)
   {
    base.OnAfterExpand(e);
    this.dataGridView.Columns[this.dataGridView.CurrentCell.ColumnIndex].Width = this.Width+;
    this.dataGridView.Rows[this.dataGridView.CurrentCell.RowIndex].Height = this.Height+;
   }
   protected override void OnAfterSelect(TreeViewEventArgs e)
   {
    // Notify the DataGridView that the contents of the cell
    // have changed.
    valueChanged = true;
    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
    base.OnAfterSelect(e);
   }
  } 

  為了在分歧類之間傳遞參數,界說一個全局靜態類:

 /// <summary>
  /// 靜態類的靜態屬性,用於在分歧class間傳遞參數
  /// </summary>
  public static class Roots
  {
   //早年台綁定樹
  public static TreeView tree = null;
  }

完全代碼為:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.ComponentModel;
 namespace Host_Controls_in_Windows_Forms_DataGridView_Cells
 {
  /// <summary>
  /// 靜態類的靜態屬性,用於在分歧class間傳遞參數
  /// </summary>
  public static class Roots
  {
   //早年台綁定樹
   public static TreeView tree = null;
  }
  /// <summary>
  /// Host TreeView In DataGridView Cell
  /// </summary>
  public class TreeViewColumn : DataGridViewColumn
  {
   public TreeViewColumn()
    : base(new TreeViewCell())
   {
   }
   [Description("Set TreeView Root in DataGridView Cell"), Category("TreeView")]
   public TreeView _root
   {
    get{return Roots.tree;}
    set{Roots.tree=value;}
   }
   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;
    // Use the default row value when Value property is null.
    if (this.Value == null)
    {
     ctl.SelectedNode =new TreeNode( this.DefaultNewRowValue.ToString());
    }
    else
    {
     ctl.SelectedNode = new TreeNode(this.Value.ToString());
    }
   }
   public override Type EditType
   {
    get
    {
     // Return the type of the editing control that CalendarCell uses.
     return typeof(TreeViewEditingControl);
    }
   }
   public override Type ValueType
   {
    get
    {
     // Return the type of the value that CalendarCell contains.
     return typeof(String);
    }
   }
   public override object DefaultNewRowValue
   {
    get
    {
     // Use the current date and time as the default value.
     return "";
    }
   }
  }
  //-----------------------------------------------------------------
 public class TreeViewEditingControl : TreeView, IDataGridViewEditingControl
  {
   DataGridView dataGridView;
   private bool valueChanged = false;
   int rowIndex;
   public TreeViewEditingControl()
   {
    try
    {
     //必需加Roots.tree.Nodes[].Clone() 不然報錯 不克不及在多處增加或拔出項,必需起首將其從以後地位移除或將其克隆
     this.Nodes.Add(Roots.tree.Nodes[].Clone() as TreeNode);
     this.SelectedNode = this.Nodes[];
    }
    catch (Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
   }
   // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
   // property.
   public object EditingControlFormattedValue
   {
    get
    {
     return this.SelectedNode.Text;
    }
    set
    {
     if (value is String)
     {
      try
      {
       // This will throw an exception of the string is 
       // null, empty, or not in the format of a date.
       this.SelectedNode = new TreeNode((String)value);
      }
      catch
      {
       // In the case of an exception, just use the 
       // default value so we're not left with a null
       // value.
       this.SelectedNode = new TreeNode("");
      }
     }
    }
   }
   // Implements the 
   // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
   public object GetEditingControlFormattedValue(
    DataGridViewDataErrorContexts context)
   {
    return EditingControlFormattedValue;
   }
   // Implements the 
   // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
   public void ApplyCellStyleToEditingControl(
    DataGridViewCellStyle dataGridViewCellStyle)
   {
    this.Font = dataGridViewCellStyle.Font;
    this.ForeColor = dataGridViewCellStyle.ForeColor;
    this.BackColor = dataGridViewCellStyle.BackColor;
   }
   // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
   // property.
   public int EditingControlRowIndex
   {
    get
    {
     return rowIndex;
    }
    set
    {
     rowIndex = value;
    }
   }
   // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
   // method.
   public bool EditingControlWantsInputKey(
    Keys key, bool dataGridViewWantsInputKey)
   {
    // Let the TreeViewPicker handle the keys listed.
    switch (key & Keys.KeyCode)
    {
     case Keys.Left:
     case Keys.Up:
     case Keys.Down:
     case Keys.Right:
     case Keys.Home:
     case Keys.End:
     case Keys.PageDown:
     case Keys.PageUp:
      return true;
     default:
      return !dataGridViewWantsInputKey;
    }
   }
   // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
   // method.
   public void PrepareEditingControlForEdit(bool selectAll)
   {
    // No preparation needs to be done.
   }
   // Implements the IDataGridViewEditingControl
   // .RepositionEditingControlOnValueChange property.
   public bool RepositionEditingControlOnValueChange
   {
    get
    {
     return false;
    }
   }
   // Implements the IDataGridViewEditingControl
   // .EditingControlDataGridView property.
   public DataGridView EditingControlDataGridView
   {
    get
    {
     return dataGridView;
    }
    set
    {
     dataGridView = value;
    }
   }
   // Implements the IDataGridViewEditingControl
   // .EditingControlValueChanged property.
   public bool EditingControlValueChanged
   {
    get
    {
     return valueChanged;
    }
    set
    {
     valueChanged = value;
    }
   }
   // Implements the IDataGridViewEditingControl
   // .EditingPanelCursor property.
   public Cursor EditingPanelCursor
   {
    get
    {
     return base.Cursor;
    }
   }
   protected override void OnAfterExpand(TreeViewEventArgs e)
   {
    base.OnAfterExpand(e);
    this.dataGridView.Columns[this.dataGridView.CurrentCell.ColumnIndex].Width = this.Width+;
    this.dataGridView.Rows[this.dataGridView.CurrentCell.RowIndex].Height = this.Height+;
   }
   protected override void OnAfterSelect(TreeViewEventArgs e)
   {
    // Notify the DataGridView that the contents of the cell
    // have changed.
    valueChanged = true;
    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
    base.OnAfterSelect(e);
   }
  }
 }

  當編纂無誤後,可以在添加列的時刻看到TreeViewColumn類型。此類型裸露出一個_root屬性,可以綁定內部的一個帶數據的TreeView。

  運轉代碼,單擊單位格,進入編纂狀況,可以看到以下界面:

以上內容是小編給年夜家引見的C#自界說DataGridViewColumn顯示TreeView 的全體論述,願望年夜家愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved