亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#自定義DataGridViewColumn顯示TreeView

2020-01-24 01:20:19
字體:
來源:轉載
供稿:網友

我們可以自定義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 的全部敘述,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
秋霞av国产精品一区| 亚洲一二三在线| 欧美精品18videosex性欧美| 亚洲综合色av| 91欧美精品午夜性色福利在线| 日韩福利在线播放| 亚洲激情在线观看视频免费| 伊人伊人伊人久久| 亚洲女人天堂网| 51视频国产精品一区二区| 在线视频日韩精品| 亚洲综合色激情五月| 最近中文字幕mv在线一区二区三区四区| 亚洲国产另类 国产精品国产免费| 美女视频久久黄| 九九久久综合网站| 91热福利电影| 久久精品成人一区二区三区| 国产精品成熟老女人| 在线播放国产精品| 欧美高清videos高潮hd| 久久久久久尹人网香蕉| 欧美在线免费看| 成人激情综合网| 久久久久久久久久久亚洲| 国产精品美女在线| 成人精品视频久久久久| 日韩一区二区欧美| 精品自在线视频| 成人疯狂猛交xxx| 欧美高清在线观看| 亚洲成人激情视频| 美女啪啪无遮挡免费久久网站| 精品少妇v888av| 国产精自产拍久久久久久| 一本色道久久88精品综合| 7777精品久久久久久| 亚洲最大在线视频| 亚洲精品在线91| 色琪琪综合男人的天堂aⅴ视频| 日本成人免费在线| 欧美日韩在线另类| 久久人人爽人人爽人人片亚洲| 国产美女久久精品| 国产精品成人播放| 国产日韩中文在线| 黄网站色欧美视频| 欧美又大粗又爽又黄大片视频| 亚洲无限乱码一二三四麻| 欧美午夜精品久久久久久久| 亚洲一区二区久久久久久| 久久久久久尹人网香蕉| 在线亚洲欧美视频| 青青草国产精品一区二区| 日韩久久午夜影院| 欧美国产日本在线| 国产精品久久久久久搜索| 亚洲欧洲视频在线| www.99久久热国产日韩欧美.com| 国产精品www网站| 91精品国产亚洲| 久久久久久免费精品| 亚洲欧美一区二区精品久久久| 精品magnet| 狠狠做深爱婷婷久久综合一区| 国产精品91免费在线| 96sao精品视频在线观看| 久久影院资源网| 欧美精品电影免费在线观看| 国产精品免费久久久久影院| 色偷偷9999www| 日韩av免费一区| 欧美激情在线有限公司| 久久精品电影一区二区| 国产91ⅴ在线精品免费观看| 久久精视频免费在线久久完整在线看| 亚洲精品视频在线播放| 国产乱人伦真实精品视频| 亚洲一品av免费观看| 国产精品免费小视频| 欧美日韩成人在线播放| 精品久久久久久中文字幕大豆网| 日韩国产欧美精品一区二区三区| 91chinesevideo永久地址| 麻豆精品精华液| 亚洲国产成人精品电影| 成人亚洲欧美一区二区三区| 狠狠干狠狠久久| 色婷婷综合久久久久中文字幕1| 日韩精品免费在线播放| 欧美视频裸体精品| 91色琪琪电影亚洲精品久久| 欧美大尺度电影在线观看| 欧美成人免费观看| 国产精品夫妻激情| 亚洲人av在线影院| www.精品av.com| 久久久国产精品视频| 久久99青青精品免费观看| 欧美日韩在线视频一区二区| 国产精品久久久久久久午夜| 国产精品亚洲一区二区三区| 精品久久在线播放| 国产精品成人一区二区| 欧美成在线观看| 国产一区二区三区在线视频| 欧美精品一区三区| 欧美日韩精品国产| 欧美一级在线亚洲天堂| 国产精品久久久久秋霞鲁丝| 欧美激情一区二区久久久| 久久这里有精品视频| 日韩免费av一区二区| 最近2019中文字幕mv免费看| 成人黄色影片在线| 欧美大片在线免费观看| 在线精品视频视频中文字幕| 在线亚洲欧美视频| 亚洲欧美日韩爽爽影院| 亚洲国产成人爱av在线播放| 日韩av网站大全| 亚洲第一av网站| 精品视频一区在线视频| 成人深夜直播免费观看| 国产玖玖精品视频| 欧美极品少妇与黑人| 亚洲天堂男人天堂女人天堂| 日韩av在线免费观看一区| 日韩av在线看| 成人免费视频xnxx.com| 中文字幕精品久久| 亚洲女人天堂视频| 欧美成人剧情片在线观看| 国产精品一区二区av影院萌芽| 亚洲免费影视第一页| 亚洲精品视频二区| 色妞一区二区三区| 欧美激情视频网| 国产精品久久久久久久久久小说| 欧美在线视频观看免费网站| 精品国产1区2区| 亚洲精品网址在线观看| 伊人伊人伊人久久| 国产精品视频自拍| 久久久亚洲国产天美传媒修理工| 国产精品国产三级国产aⅴ9色| 中文字幕亚洲欧美日韩2019| 久久精品2019中文字幕| 日韩hd视频在线观看| 亚洲欧美日韩在线高清直播| 国产亚洲视频在线观看| 国产成人jvid在线播放| 国产精品久久久久久久久久尿| 亚洲精品欧美日韩| 国产精品高潮在线| 色综合色综合久久综合频道88| 亚洲精品999| 欧美性在线观看| 欧美肥婆姓交大片| 国产中文字幕亚洲| 欧美黄色免费网站| 亚洲欧美另类国产| 亚洲成人激情图| 日韩视频精品在线|