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

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

C#如何在DataGridViewCell中自定義腳本編輯器

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

    上一篇博文探討了如何自定義DataGridViewColumn實現一個TreeViewColumn來在DataGridView控件中顯示TreeView控件,其實我們還可以繼續發揮想象,自定義其他的列類型,下面介紹一個腳本編輯器列類型,我這里取名ScriptTextEditorColumn,當用戶單擊DataGridView的ScriptTextEditorColumn時,單元格右邊會出現一個按鈕,單擊按鈕會彈出一個腳本編輯器窗體,用戶可以在窗體中進行代碼維護,然后回寫到單元格中。

  用人會問,這個控件有啥實際作用,其實結合動態編譯的技術,在datagridview中進行取值公式的模板設定,也就是在對應的單元格中設置C#腳本,然后動態執行后呈現結果到一個datagridview單元格中,這樣就實現了動態配置datagridview后臺計算邏輯的目的,當然實現這樣的功能還需要大量的工作,但是主要的思路就是這樣。

1 ScriptTextEditorColumn

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Windows.Forms;  6   7 namespace Host_Controls_in_Windows_Forms_DataGridView_Cells  8 {  9    public class ScriptTextEditorColumn : DataGridViewColumn 10     { 11        public ScriptTextEditorColumn() 12            : base(new ScriptTextEditorCell()) 13         { 14         } 15  16         public override DataGridViewCell CellTemplate 17         { 18             get 19             { 20                 return base.CellTemplate; 21             } 22             set 23             { 24                 // Ensure that the cell used for the template is a ScriptTextEditorCell. 25                 if (value != null && 26                     !value.GetType().IsAssignableFrom(typeof(ScriptTextEditorCell))) 27                 { 28                     throw new InvalidCastException("Must be a ScriptTextEditorCell"); 29                 } 30                 base.CellTemplate = value; 31             } 32         } 33     } 34  35     //---------------------------------------------------------------------- 36     public class  ScriptTextEditorCell : DataGridViewTextBoxCell 37     { 38  39         public ScriptTextEditorCell() 40             : base() 41         { 42             43         } 44  45         public override void InitializeEditingControl(int rowIndex, object 46             initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 47         { 48             // Set the value of the editing control to the current cell value. 49             base.InitializeEditingControl(rowIndex, initialFormattedValue, 50                 dataGridViewCellStyle); 51             ScriptTextEditingControl ctl = 52                 DataGridView.EditingControl as ScriptTextEditingControl; 53             // Use the default row value when Value PRoperty is null. 54             if (this.Value == null) 55             { 56                 ctl.textBox1.Text = (String)this.DefaultNewRowValue; 57             } 58             else 59             { 60                 ctl.textBox1.Text = (String)this.Value; 61             } 62         } 63  64         public override Type EditType 65         { 66             get 67             { 68                 // Return the type of the editing control that CalendarCell uses. 69                 return typeof(ScriptTextEditingControl); 70             } 71         } 72  73         public override Type ValueType 74         { 75             get 76             { 77                 // Return the type of the value that CalendarCell contains. 78  79                 return typeof(String); 80             } 81         } 82  83         public override object DefaultNewRowValue 84         { 85             get 86             { 87                 // Use the current date and time as the default value. 88                 string code = @" 89 #region 90 //jackwangcumt 91 #endregion 92 using System; 93 using System.Collections.Generic; 94 using System.ComponentModel; 95 using System.Drawing; 96 using System.Data; 97 using System.Linq; 98 using System.Text; 99 using System.Windows.Forms;100 namespace Host_Controls_in_Windows_Forms_DataGridView_Cells101 {102     public partial class SourceTextBox : UserControl103     {104         public SourceTextBox()105         {106             InitializeComponent();107             this.textBox1.Location = this.Location;108             this.textBox1.Width = this.Width;109             this.textBox1.Height = this.Height;110         }111         protected void OnValueChanged(string text)112         {113             this.textBox1.Text = text;114         }115 116         private void btnSource_Click(object sender, EventArgs e)117         {118             ScriptEditor frm = new ScriptEditor(this.textBox1.Text);119             frm.ShowDialog();120             this.textBox1.Text = frm.fastColoredTextBox1.Text;121         }122     }123 }124                 ";125                 return code;126             }127         }128     }129     //-----------------------------------------------------------------130 131     class ScriptTextEditingControl : SourceTextBox, IDataGridViewEditingControl132     {133         DataGridView dataGridView;134         private bool valueChanged = false;135         int rowIndex;136 137         public ScriptTextEditingControl()138         {139             //文本變更更新到cell140             this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);141         }142 143         void textBox1_TextChanged(object sender, EventArgs e)144         {145             // Notify the DataGridView that the contents of the cell146             // have changed.147             valueChanged = true;148             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);149             //調用SourceTextBox的OnValueChanged(string Text)150             base.OnValueChanged(this.textBox1.Text);151         }152 153         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 154         // property.155         public object EditingControlFormattedValue156         {157             get158             {159                 return this.textBox1.Text;160             }161             set162             {163                 if (value is String)164                 {165                     try166                     {167                         // This will throw an exception of the string is 168                         // null, empty, or not in the format of a date.169                        this.textBox1.Text=((String)value);170                     }171                     catch172                     {173                         // In the case of an exception, just use the 174                         // default value so we're not left with a null175                         // value.176                         this.textBox1.Text = "jackwangcumt>>error";177                     }178                 }179             }180         }181 182         // Implements the 183         // IDataGridViewEditingControl.GetEditingControlFormattedValue method.184         public object GetEditingControlFormattedValue(185             DataGridViewDataErrorContexts context)186         {187             return EditingControlFormattedValue;188         }189 190         // Implements the 191         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.192         public void ApplyCellStyleToEditingControl(193             DataGridViewCellStyle dataGridViewCellStyle)194         {195             this.Font = dataGridViewCellStyle.Font;196             //this.CalendarForeColor = dataGridViewCellStyle.ForeColor;197             //this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;198         }199 200         // Implements the IDataGridViewEditingControl.EditingControlRowIndex 201         // property.202         public int EditingControlRowIndex203         {204             get205             {206                 return rowIndex;207             }208             set209             {210                 rowIndex = value;211             }212         }213 214         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 215         // method.216         public bool EditingControlWantsInputKey(217             Keys key, bool dataGridViewWantsInputKey)218         {219             // Let the DateTimePicker handle the keys listed.220             switch (key & Keys.KeyCode)221             {222                 case Keys.Left:223                 case Keys.Up:224                 case Keys.Down:225                 case Keys.Right:226                 case Keys.Home:227                 case Keys.End:228                 case Keys.PageDown:229                 case Keys.PageUp:230                     return true;231                 default:232                     return !dataGridViewWantsInputKey;233             }234         }235 236         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 237         // method.238         public void PrepareEditingControlForEdit(bool selectAll)239         {240             // No preparation needs to be done.241         }242 243         // Implements the IDataGridViewEditingControl244         // .RepositionEditingControlOnValueChange property.245         public bool RepositionEditingControlOnValueChange246         {247             get248             {249                 return false;250             }251         }252 253         // Implements the IDataGridViewEditingControl254         // .EditingControlDataGridView property.255         public DataGridView EditingControlDataGridView256         {257             get258             {259                 return dataGridView;260             }261             set262             {263                 dataGridView = value;264             }265         }266 267         // Implements the IDataGridViewEditingControl268         // .EditingControlValueChanged property.269         public bool EditingControlValueChanged270         {271             get272             {273                 return valueChanged;274             }275             set276             {277                 valueChanged = value;278             }279         }280 281         // Implements the IDataGridViewEditingControl282         // .EditingPanelCursor property.283         public Cursor EditingPanelCursor284         {285             get286             {287                 return base.Cursor;288             }289         }290 291         protected override void OnTextChanged(EventArgs e)292         {293             // Notify the DataGridView that the contents of the cell294             // have changed.295             valueChanged = true;296             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);297             base.OnTextChanged(e);298           299         }300    301     }302 303 304 305 }

2 SourceTextBox

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Host_Controls_in_Windows_Forms_DataGridView_Cells11 {12     public partial class SourceTextBox : UserControl13     {14         public SourceTextBox()15         {16             InitializeComponent();17             this.textBox1.Location = this.Location;18             this.textBox1.Width = this.Width;19             this.textBox1.Height = this.Height;20         }21         protected void OnValueChanged(string text)22         {23             this.textBox1.Text = text;24         }25 26         private void btnSource_Click(object sender, EventArgs e)27         {28             ScriptEditor frm = new ScriptEditor(this.textBox1.Text);29             frm.ShowDialog();30             this.textBox1.Text = frm.fastColoredTextBox1.Text;31         }32     }33 }
 1 namespace Host_Controls_in_Windows_Forms_DataGridView_Cells 2 { 3     partial class SourceTextBox 4     { 5         /// <summary>  6         /// 必需的設計器變量。 7         /// </summary> 8         private System.ComponentModel.IContainer components = null; 9 10         /// <summary> 11         /// 清理所有正在使用的資源。12         /// </summary>13         /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>14         protected override void Dispose(bool disposing)15         {16             if (disposing && (components != null))17             {18                 components.Dispose();19             }20             base.Dispose(disposing);21         }22 23         #region 組件設計器生成的代碼24 25         /// <summary> 26         /// 設計器支持所需的方法 - 不要27         /// 使用代碼編輯器修改此方法的內容。28         /// </summary>29         private void InitializeComponent()30         {31             this.textBox1 = new System.Windows.Forms.TextBox();32             this.btnSource = new System.Windows.Forms.Button();33             this.SuspendLayout();34             // 35             // textBox136             // 37             this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;38             this.textBox1.Location = new System.Drawing.Point(3, 3);39             this.textBox1.Margin = new System.Windows.Forms.Padding(4);40             this.textBox1.Multiline = true;41             this.textBox1.Name = "textBox1";42             this.textBox1.Size = new System.Drawing.Size(175, 21);43             this.textBox1.TabIndex = 1;44             // 45             // btnSource46             // 47             this.btnSource.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)48                         | System.Windows.Forms.AnchorStyles.Right)));49             this.btnSource.BackColor = System.Drawing.Color.Transparent;50             this.btnSource.BackgroundImage = global::Host_Controls_in_Windows_Forms_DataGridView_Cells.Resource.setting;51             this.btnSource.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;52             this.btnSource.FlatAppearance.BorderColor = System.Drawing.Color.White;53             this.btnSource.FlatAppearance.BorderSize = 0;54             this.btnSource.FlatStyle = System.Windows.Forms.FlatStyle.Flat;55             this.btnSource.Font = new System.Drawing.Font("新宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));56             this.btnSource.Location = new System.Drawing.Point(159, -1);57             this.btnSource.Margin = new System.Windows.Forms.Padding(0);58             this.btnSource.Name = "btnSource";59             this.btnSource.Size = new System.Drawing.Size(19, 25);60             this.btnSource.TabIndex = 0;61             this.btnSource.UseVisualStyleBackColor = false;62             this.btnSource.Click += new System.EventHandler(this.btnSource_Click);63             // 64             // SourceTextBox65             // 66             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);67             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;68             this.Controls.Add(this.btnSource);69             this.Controls.Add(this.textBox1);70             this.Margin = new System.Windows.Forms.Padding(0);71             this.Name = "SourceTextBox";72             this.Size = new System.Drawing.Size(178, 26);73             this.ResumeLayout(false);74             this.PerformLayout();75 76         }77 78         #endregion79 80         public System.Windows.Forms.Button btnSource;81         public System.Windows.Forms.TextBox textBox1;82     }83 }
View Code

3 效果

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩精品免费在线播放| 国产一区二区三区在线播放免费观看| 久久人人爽人人爽人人片亚洲| 国产成人av在线播放| 日韩av黄色在线观看| 亚洲男人天天操| 色www亚洲国产张柏芝| 亚洲一级黄色av| 国产精品pans私拍| 亚洲自拍偷拍在线| 日韩的一区二区| 国产区亚洲区欧美区| 尤物九九久久国产精品的特点| 午夜欧美大片免费观看| 久久精品国产欧美激情| 91精品视频观看| 日韩一区二区三区xxxx| 亚洲区在线播放| 久久综合电影一区| 久久久国产成人精品| 久久伊人免费视频| 国产成人中文字幕| 久久偷看各类女兵18女厕嘘嘘| 九色91av视频| 亚洲一二在线观看| 日韩视频免费大全中文字幕| 国产精品电影一区| 91久久国产精品91久久性色| 精品久久久一区| 亚洲美女免费精品视频在线观看| 日韩精品免费在线播放| 高清欧美性猛交| 亚洲精品一区二区网址| 国产自产女人91一区在线观看| 国产欧美日韩高清| 亚洲成人国产精品| 国产精品久久久久免费a∨大胸| 国产欧美日韩专区发布| 欧美体内谢she精2性欧美| 欧美性资源免费| 97在线视频免费播放| 国产亚洲免费的视频看| 亚洲字幕一区二区| 国产精品一久久香蕉国产线看观看| 午夜精品免费视频| 久久久精品视频成人| 国产亚洲成av人片在线观看桃| 亚洲成人av中文字幕| 欧美电影在线观看完整版| 亚洲电影免费观看高清完整版在线| 国产亚洲欧洲黄色| 精品视频偷偷看在线观看| 欧美日韩免费观看中文| 亚洲欧美一区二区三区久久| 亚洲精品一区二三区不卡| 日韩av在线导航| 亚洲精品国产品国语在线| 国产一区视频在线播放| 欧美电影免费在线观看| 亚洲美女av在线播放| 亚洲伊人一本大道中文字幕| 色综合久综合久久综合久鬼88| 亚洲国产精品视频在线观看| 疯狂做受xxxx高潮欧美日本| 日韩中文字幕在线精品| 精品久久久一区| 亚洲二区中文字幕| 98精品国产自产在线观看| 操人视频在线观看欧美| 日韩中文字幕视频| 色偷偷偷亚洲综合网另类| 日本a级片电影一区二区| 日本国产欧美一区二区三区| 国产精品电影一区| 久久精品在线播放| 亚洲日本成人女熟在线观看| 久久免费精品视频| 久久久久成人精品| 欧美日韩国产一中文字不卡| 久久久久久久久久亚洲| 亚洲自拍另类欧美丝袜| 日韩一区二区精品视频| 91久久精品日日躁夜夜躁国产| 91精品久久久久久综合乱菊| 蜜臀久久99精品久久久久久宅男| 亚洲xxxx视频| 欧美日韩国产在线| 夜夜嗨av色一区二区不卡| 久久久国产精品亚洲一区| 久久躁狠狠躁夜夜爽| 色中色综合影院手机版在线观看| 精品久久久久久电影| 18一19gay欧美视频网站| 精品国产一区二区在线| 亚洲第一福利在线观看| 日韩精品高清视频| 国模视频一区二区三区| 97视频在线观看网址| 亚洲美女视频网| 欧美一区二区大胆人体摄影专业网站| 一本大道久久加勒比香蕉| 欧美性极品xxxx做受| 亚洲qvod图片区电影| 日本高清视频精品| 国内精品中文字幕| 国产女同一区二区| 国内精品400部情侣激情| 中文字幕少妇一区二区三区| 一本色道久久综合狠狠躁篇的优点| 久久精品国产v日韩v亚洲| 国产精品极品美女在线观看免费| 国产欧美日韩中文字幕在线| 韩剧1988免费观看全集| 九九久久精品一区| 91视频九色网站| 日韩va亚洲va欧洲va国产| 91在线国产电影| 日韩性生活视频| 亚洲美女激情视频| 精品亚洲永久免费精品| 亚洲一区二区三区在线视频| 国产精品爱啪在线线免费观看| 日本19禁啪啪免费观看www| 米奇精品一区二区三区在线观看| 在线观看亚洲区| 欧美另类第一页| 亚洲福利小视频| 亚洲第一区中文99精品| 国产精品久久综合av爱欲tv| 日韩av免费网站| 久久高清视频免费| 国语自产精品视频在线看| 国产综合香蕉五月婷在线| 国产精品久久久久久超碰| 久久91超碰青草是什么| 亚洲无限乱码一二三四麻| 日韩欧美国产视频| 在线视频日韩精品| 欧美高清电影在线看| 66m—66摸成人免费视频| 欧美wwwxxxx| 亚洲日韩中文字幕在线播放| 精品电影在线观看| 精品亚洲一区二区三区在线观看| 97视频在线观看视频免费视频| 日韩av中文字幕在线免费观看| 亚洲免费小视频| 欧美壮男野外gaytube| 欧美日韩色婷婷| 亚洲天堂男人的天堂| 欧美精品久久久久| 91天堂在线观看| 一区三区二区视频| 国产精品欧美风情| 日本精品免费观看| 亚洲免费av电影| 日韩中文字幕在线免费观看| 欧美性猛交xxx| 亚洲级视频在线观看免费1级| 亚洲视频综合网| 亚洲人午夜精品免费| 国产精品99久久久久久久久久久久| 日本三级久久久| 亚洲精品久久久一区二区三区|