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

首頁 > 學院 > 開發設計 > 正文

C#如何自定義DataGridViewColumn來顯示TreeView

2019-11-14 13:41:50
字體:
來源:轉載
供稿:網友

  我們可以自定義DataGridView的DataGridViewColumn來實現自定義的列,下面介紹一下如何通過擴展DataGridViewColumn來實現一個TreeViewColumn

1 TreeViewColumn類

 TreeViewColumn繼承自DataGridViewColumn,為了動態給TreeViewColumn傳入一個TreeView,這里暴露出一個公共屬性_root,可以綁定一個初始化的TreeView. 另外需要重寫DataGridCell類型的CellTemplate,這里返還一個TreeViewCell(需要自定義)

 1     /// <summary> 2     /// Host TreeView In DataGridView Cell 3     /// </summary> 4    public class TreeViewColumn : DataGridViewColumn 5     { 6         public TreeViewColumn() 7             : base(new TreeViewCell()) 8         { 9         }10         [Description("Set TreeView Root in DataGridView Cell"), Category("TreeView")]11         public TreeView _root12         {13             get{return Roots.tree;}14             set{Roots.tree=value;}15         }16         public override DataGridViewCell CellTemplate17         {18             get19             {20                 return base.CellTemplate;21             }22             set23             {24                 // Ensure that the cell used for the template is a TreeViewCell.25                 if (value != null &&26                     !value.GetType().IsAssignableFrom(typeof(TreeViewCell)))27                 {28                     throw new InvalidCastException("Must be a TreeViewCell");29                 }30                 base.CellTemplate = value;31             }32         }33     }

2 TreeViewCell類

    上面TreeViewColumn重寫了CellTemplate,返回的就是自定義的TreeViewCell,這里就是具體實現其邏輯。一般來說選擇樹控件的節點后,返回的是一個文本信息,是文本類型,可以繼承DataGridViewTextBoxCell,并重寫InitializeEditingControl來進行自定義的DataGridView.EditingControl (編輯控件)。

 1   public class TreeViewCell : DataGridViewTextBoxCell 2     { 3  4         public TreeViewCell() 5             : base() 6         { 7          8             //初始設置 9         }10        11         public override void InitializeEditingControl(int rowIndex, object12             initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)13         {14             // Set the value of the editing control to the current cell value.15             base.InitializeEditingControl(rowIndex, initialFormattedValue,16                 dataGridViewCellStyle);17             TreeViewEditingControl ctl =18                 DataGridView.EditingControl as TreeViewEditingControl;19             // Use the default row value when Value PRoperty is null.20             if (this.Value == null)21             {22 23                 ctl.SelectedNode =new TreeNode( this.DefaultNewRowValue.ToString());24             }25             else26             {27                 ctl.SelectedNode = new TreeNode(this.Value.ToString());28             }29         }30 31         public override Type EditType32         {33             get34             {35                 // Return the type of the editing control that CalendarCell uses.36                 return typeof(TreeViewEditingControl);37             }38         }39 40         public override Type ValueType41         {42             get43             {44                 // Return the type of the value that CalendarCell contains.45                 return typeof(String);46             }47         }48 49         public override object DefaultNewRowValue50         {51             get52             {53                 // Use the current date and time as the default value.54                 return "";55             }56         }57     }

3 TreeViewEditingControl類

  TreeViewEditingControl為編輯控件,當用戶編輯TreeViewCell時,顯示的為樹編輯控件,需要繼承TreeView,同時實現IDataGridViewEditingControl接口,實現以下方法:

  1 public class TreeViewEditingControl : TreeView, IDataGridViewEditingControl  2     {  3         DataGridView dataGridView;  4         private bool valueChanged = false;  5         int rowIndex;  6         public TreeViewEditingControl()  7         {  8             try  9             { 10                 //必須加Roots.tree.Nodes[0].Clone() 否則報錯 不能在多處增添或插入項,必須首先將其從當前位置移除或將其克隆 11                 this.Nodes.Add(Roots.tree.Nodes[0].Clone() as TreeNode); 12                 this.SelectedNode = this.Nodes[0]; 13  14                15             } 16             catch (Exception ex) 17             { 18                 MessageBox.Show(ex.Message); 19             } 20             21           22         } 23     24         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue  25         // property. 26         public object EditingControlFormattedValue 27         { 28             get 29             { 30                 return this.SelectedNode.Text; 31             } 32             set 33             { 34                 if (value is String) 35                 { 36                     try 37                     { 38                         // This will throw an exception of the string is  39                         // null, empty, or not in the format of a date. 40                         this.SelectedNode = new TreeNode((String)value); 41                          42                     } 43                     catch 44                     { 45                         // In the case of an exception, just use the  46                         // default value so we're not left with a null 47                         // value. 48                         this.SelectedNode = new TreeNode(""); 49                     } 50                 } 51             } 52         } 53  54         // Implements the  55         // IDataGridViewEditingControl.GetEditingControlFormattedValue method. 56         public object GetEditingControlFormattedValue( 57             DataGridViewDataErrorContexts context) 58         { 59             return EditingControlFormattedValue; 60         } 61  62         // Implements the  63         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method. 64         public void ApplyCellStyleToEditingControl( 65             DataGridViewCellStyle dataGridViewCellStyle) 66         { 67             this.Font = dataGridViewCellStyle.Font; 68             this.ForeColor = dataGridViewCellStyle.ForeColor; 69             this.BackColor = dataGridViewCellStyle.BackColor; 70         } 71  72         // Implements the IDataGridViewEditingControl.EditingControlRowIndex  73         // property. 74         public int EditingControlRowIndex 75         { 76             get 77             { 78                 return rowIndex; 79             } 80             set 81             { 82                 rowIndex = value; 83             } 84         } 85  86         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey  87         // method. 88         public bool EditingControlWantsInputKey( 89             Keys key, bool dataGridViewWantsInputKey) 90         { 91             // Let the TreeViewPicker handle the keys listed. 92             switch (key & Keys.KeyCode) 93             { 94                 case Keys.Left: 95                 case Keys.Up: 96                 case Keys.Down: 97                 case Keys.Right: 98                 case Keys.Home: 99                 case Keys.End:100                 case Keys.PageDown:101                 case Keys.PageUp:102                     return true;103                 default:104                     return !dataGridViewWantsInputKey;105             }106         }107 108         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 109         // method.110         public void PrepareEditingControlForEdit(bool selectAll)111         {112             // No preparation needs to be done.113         }114 115         // Implements the IDataGridViewEditingControl116         // .RepositionEditingControlOnValueChange property.117         public bool RepositionEditingControlOnValueChange118         {119             get120             {121                 return false;122             }123         }124 125         // Implements the IDataGridViewEditingControl126         // .EditingControlDataGridView property.127         public DataGridView EditingControlDataGridView128         {129             get130             {131                 return dataGridView;132             }133             set134             {135                 dataGridView = value;136             }137         }138 139         // Implements the IDataGridViewEditingControl140         // .EditingControlValueChanged property.141         public bool EditingControlValueChanged142         {143             get144             {145                 return valueChanged;146             }147             set148             {149                 valueChanged = value;150             }151         }152 153         // Implements the IDataGridViewEditingControl154         // .EditingPanelCursor property.155         public Cursor EditingPanelCursor156         {157             get158             {159                 return base.Cursor;160             }161         }162 163         protected override void OnAfterExpand(TreeViewEventArgs e)164         {165             base.OnAfterExpand(e);166             this.dataGridView.Columns[this.dataGridView.CurrentCell.ColumnIndex].Width = this.Width+10;167             this.dataGridView.Rows[this.dataGridView.CurrentCell.RowIndex].Height = this.Height+20;168            169         }170         protected override void OnAfterSelect(TreeViewEventArgs e)171         {172             // Notify the DataGridView that the contents of the cell173             // have changed.174             valueChanged = true;175             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);176             base.OnAfterSelect(e);177             178         }179        180     }

  為了在不同類之間傳遞參數,定義一個全局靜態類:

1   /// <summary>2     /// 靜態類的靜態屬性,用于在不同class間傳遞參數3     /// </summary>4     public static class Roots5     {6         //從前臺綁定樹7        public static TreeView tree = null;8     }

完整代碼為:

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Windows.Forms;  6 using System.ComponentModel;  7 namespace Host_Controls_in_Windows_Forms_DataGridView_Cells  8 {  9     /// <summary> 10     /// 靜態類的靜態屬性,用于在不同class間傳遞參數 11     /// </summary> 12     public static class Roots 13     { 14         //從前臺綁定樹 15        public static TreeView tree = null; 16     } 17     /// <summary> 18     /// Host TreeView In DataGridView Cell 19     /// </summary> 20    public class TreeViewColumn : DataGridViewColumn 21     { 22         public TreeViewColumn() 23             : base(new TreeViewCell()) 24         { 25         } 26         [Description("Set TreeView Root in DataGridView Cell"), Category("TreeView")] 27         public TreeView _root 28         { 29             get{return Roots.tree;} 30             set{Roots.tree=value;} 31         } 32         public override DataGridViewCell CellTemplate 33         { 34             get 35             { 36                 return base.CellTemplate; 37             } 38             set 39             { 40                 // Ensure that the cell used for the template is a TreeViewCell. 41                 if (value != null && 42                     !value.GetType().IsAssignableFrom(typeof(TreeViewCell))) 43                 { 44                     throw new InvalidCastException("Must be a TreeViewCell"); 45                 } 46                 base.CellTemplate = value; 47             } 48         } 49     } 50  51     //---------------------------------------------------------------------- 52     public class TreeViewCell : DataGridViewTextBoxCell 53     { 54  55         public TreeViewCell() 56             : base() 57         { 58          59             //初始設置 60         } 61         62         public override void InitializeEditingControl(int rowIndex, object 63             initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 64         { 65             // Set the value of the editing control to the current cell value. 66             base.InitializeEditingControl(rowIndex, initialFormattedValue, 67                 dataGridViewCellStyle); 68             TreeViewEditingControl ctl = 69                 DataGridView.EditingControl as TreeViewEditingControl; 70             // Use the default row value when Value property is null. 71             if (this.Value == null) 72             { 73  74                 ctl.SelectedNode =new TreeNode( this.DefaultNewRowValue.ToString()); 75             } 76             else 77             { 78                 ctl.SelectedNode = new TreeNode(this.Value.ToString()); 79             } 80         } 81  82         public override Type EditType 83         { 84             get 85             { 86                 // Return the type of the editing control that CalendarCell uses. 87                 return typeof(TreeViewEditingControl); 88             } 89         } 90  91         public override Type ValueType 92         { 93             get 94             { 95                 // Return the type of the value that CalendarCell contains. 96                 return typeof(String); 97             } 98         } 99 100         public override object DefaultNewRowValue101         {102             get103             {104                 // Use the current date and time as the default value.105                 return "";106             }107         }108     }109     //-----------------------------------------------------------------110 111    public class TreeViewEditingControl : TreeView, IDataGridViewEditingControl112     {113         DataGridView dataGridView;114         private bool valueChanged = false;115         int rowIndex;116         public TreeViewEditingControl()117         {118             try119             {120                 //必須加Roots.tree.Nodes[0].Clone() 否則報錯 不能在多處增添或插入項,必須首先將其從當前位置移除或將其克隆121                 this.Nodes.Add(Roots.tree.Nodes[0].Clone() as TreeNode);122                 this.SelectedNode = this.Nodes[0];123 124               125             }126             catch (Exception ex)127             {128                 MessageBox.Show(ex.Message);129             }130            131          132         }133    134         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 135         // property.136         public object EditingControlFormattedValue137         {138             get139             {140                 return this.SelectedNode.Text;141             }142             set143             {144                 if (value is String)145                 {146                     try147                     {148                         // This will throw an exception of the string is 149                         // null, empty, or not in the format of a date.150                         this.SelectedNode = new TreeNode((String)value);151                         152                     }153                     catch154                     {155                         // In the case of an exception, just use the 156                         // default value so we're not left with a null157                         // value.158                         this.SelectedNode = new TreeNode("");159                     }160                 }161             }162         }163 164         // Implements the 165         // IDataGridViewEditingControl.GetEditingControlFormattedValue method.166         public object GetEditingControlFormattedValue(167             DataGridViewDataErrorContexts context)168         {169             return EditingControlFormattedValue;170         }171 172         // Implements the 173         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.174         public void ApplyCellStyleToEditingControl(175             DataGridViewCellStyle dataGridViewCellStyle)176         {177             this.Font = dataGridViewCellStyle.Font;178             this.ForeColor = dataGridViewCellStyle.ForeColor;179             this.BackColor = dataGridViewCellStyle.BackColor;180         }181 182         // Implements the IDataGridViewEditingControl.EditingControlRowIndex 183         // property.184         public int EditingControlRowIndex185         {186             get187             {188                 return rowIndex;189             }190             set191             {192                 rowIndex = value;193             }194         }195 196         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 197         // method.198         public bool EditingControlWantsInputKey(199             Keys key, bool dataGridViewWantsInputKey)200         {201             // Let the TreeViewPicker handle the keys listed.202             switch (key & Keys.KeyCode)203             {204                 case Keys.Left:205                 case Keys.Up:206                 case Keys.Down:207                 case Keys.Right:208                 case Keys.Home:209                 case Keys.End:210                 case Keys.PageDown:211                 case Keys.PageUp:212                     return true;213                 default:214                     return !dataGridViewWantsInputKey;215             }216         }217 218         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 219         // method.220         public void PrepareEditingControlForEdit(bool selectAll)221         {222             // No preparation needs to be done.223         }224 225         // Implements the IDataGridViewEditingControl226         // .RepositionEditingControlOnValueChange property.227         public bool RepositionEditingControlOnValueChange228         {229             get230             {231                 return false;232             }233         }234 235         // Implements the IDataGridViewEditingControl236         // .EditingControlDataGridView property.237         public DataGridView EditingControlDataGridView238         {239             get240             {241                 return dataGridView;242             }243             set244             {245                 dataGridView = value;246             }247         }248 249         // Implements the IDataGridViewEditingControl250         // .EditingControlValueChanged property.251         public bool EditingControlValueChanged252         {253             get254             {255                 return valueChanged;256             }257             set258             {259                 valueChanged = value;260             }261         }262 263         // Implements the IDataGridViewEditingControl264         // .EditingPanelCursor property.265         public Cursor EditingPanelCursor266         {267             get268             {269                 return base.Cursor;270             }271         }272 273         protected override void OnAfterExpand(TreeViewEventArgs e)274         {275             base.OnAfterExpand(e);276             this.dataGridView.Columns[this.dataGridView.CurrentCell.ColumnIndex].Width = this.Width+10;277             this.dataGridView.Rows[this.dataGridView.CurrentCell.RowIndex].Height = this.Height+20;278            279         }280         protected override void OnAfterSelect(TreeViewEventArgs e)281         {282             // Notify the DataGridView that the contents of the cell283             // have changed.284             valueChanged = true;285             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);286             base.OnAfterSelect(e);287             288         }289        290     }291 292 293 294 }
View Code

  當編輯無誤后,可以在添加列的時候看到TreeViewColumn類型。此類型暴露出一個_root屬性,可以綁定外部的一個帶數據的TreeView。

  運行代碼,單擊單元格,進入編輯狀態,可以看到如下界面:

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲激情 国产| 亚洲欧美制服丝袜| 久久精品国产亚洲精品2020| 欧美性xxxxx极品娇小| 久久91精品国产91久久跳| 精品中文字幕在线观看| 国产成一区二区| 亚洲国产精品福利| 亚洲美女av在线| 91精品在线播放| 成人淫片在线看| 国产精品狼人色视频一区| 久久久精品国产一区二区| 在线视频精品一| 91豆花精品一区| 午夜精品久久久久久99热| 国产精品久久久久久久久久三级| 在线观看欧美视频| 国产精品成久久久久三级| 欧美老女人在线视频| 日韩成人中文字幕在线观看| 日韩精品福利在线| 欧美激情精品久久久久久变态| 欧美激情一区二区三区成人| 性欧美视频videos6一9| 中文字幕亚洲综合久久筱田步美| 亚洲精品一区二区久| 欧美亚州一区二区三区| 亚洲图片欧美午夜| 亚洲乱亚洲乱妇无码| 欧美自拍大量在线观看| 国产精彩精品视频| 日韩亚洲精品电影| 欧美日韩成人在线观看| 日韩h在线观看| 久热精品视频在线免费观看| 欧美日韩ab片| 成人日韩在线电影| 国产欧美精品日韩精品| 欧美综合一区第一页| 国产丝袜一区二区三区免费视频| 欧洲亚洲女同hd| 欧美黑人xxxⅹ高潮交| 日韩国产欧美精品在线| 亚洲高清久久网| 欧美午夜美女看片| 亚洲一区二区三区成人在线视频精品| 国产一区二区三区三区在线观看| 亚洲国产成人精品久久| 亚洲欧美日韩国产中文专区| 日韩欧美主播在线| 精品国产91乱高清在线观看| 尤物99国产成人精品视频| 色悠悠久久88| 日韩经典第一页| 日韩在线视频一区| 国产精品一区二区三区久久久| 欧美日韩亚洲成人| 亚洲综合在线播放| 黄色成人av在线| 精品综合久久久久久97| 97精品视频在线播放| 国产脚交av在线一区二区| 国产精品中文在线| 色狠狠av一区二区三区香蕉蜜桃| 久久精品精品电影网| 在线色欧美三级视频| 91精品久久久久久久久中文字幕| 日韩一区二区三区在线播放| 国产999精品久久久| 国产精品美女www| 亚洲欧美国产另类| 亚洲国产精品女人久久久| 国产精品高清网站| 在线精品播放av| 欧美在线激情网| 国语自产偷拍精品视频偷| 亚洲精品自拍偷拍| 亚洲影院色在线观看免费| 日韩精品在线播放| 久久久久久av| 中文一区二区视频| 91久久精品美女高潮| 国内精品久久久久| 奇米一区二区三区四区久久| 日韩大片在线观看视频| 精品一区二区三区四区| 一区二区在线免费视频| 国产精品久久电影观看| 午夜精品久久17c| 国产成人精品久久| 中文字幕日韩精品在线| 亚洲精品av在线播放| 亚洲成avwww人| 97精品一区二区三区| 精品在线观看国产| 久久久久久av| 欧美又大又硬又粗bbbbb| 国模精品视频一区二区三区| 欧美精品一二区| 国产日韩在线看| 国产成人久久精品| 国产免费一区二区三区香蕉精| 欧美激情一级精品国产| 国产精品一区二区久久久久| 欧美成人精品三级在线观看| 精品亚洲一区二区三区四区五区| 中文字幕久热精品视频在线| 欧美精品在线免费播放| 91精品视频在线免费观看| 疯狂做受xxxx欧美肥白少妇| 韩国日本不卡在线| 欧美激情精品久久久久久变态| 在线观看欧美视频| 欧美日韩激情视频8区| 92版电视剧仙鹤神针在线观看| 成人黄色在线免费| 在线观看视频亚洲| 国产精品日韩欧美综合| 日韩男女性生活视频| 欧美综合一区第一页| 色悠久久久久综合先锋影音下载| 国产999视频| 91久久久久久久久| 中文字幕av一区二区三区谷原希美| 92看片淫黄大片欧美看国产片| 亚洲aⅴ日韩av电影在线观看| 国产香蕉一区二区三区在线视频| 91久久国产综合久久91精品网站| 欧美在线视频一区二区| 久久久久一本一区二区青青蜜月| 91sao在线观看国产| 亚洲人高潮女人毛茸茸| 欧美电影在线观看| 一色桃子一区二区| 国产精品99久久99久久久二8| 欧美性开放视频| 成人网址在线观看| 国产免费一区视频观看免费| 亚洲无线码在线一区观看| 日韩av综合网| 国语自产精品视频在线看一大j8| 欧美视频一区二区三区…| 中文字幕亚洲一区二区三区五十路| 欧美成人手机在线| 久久精品国产久精国产思思| 136fldh精品导航福利| 热久久免费视频精品| 成人免费视频xnxx.com| 亚洲影院色在线观看免费| 亚洲精品av在线播放| 欧美性猛交xxxx偷拍洗澡| 亚洲综合中文字幕68页| 日韩在线视频观看| 亚洲男女性事视频| 欧美激情二区三区| 久久九九有精品国产23| 亚洲视频网站在线观看| 亚洲美女www午夜| 国产精品入口免费视频一| 国产免费一区二区三区香蕉精| 中文字幕成人精品久久不卡| 亚洲最大福利视频网站| 国产精品亚洲网站|