ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> C#教程 >> C#如何在DataGridViewCell中自定义脚本编辑器

C#如何在DataGridViewCell中自定义脚本编辑器(1/2)

来源:网络整理     时间:2015-12-16     关键词:UserControl,Exception

本篇文章主要介绍了"C#如何在DataGridViewCell中自定义脚本编辑器",主要涉及到UserControl,Exception方面的内容,对于C#教程感兴趣的同学可以参考一下:   上一篇博文探讨了如何自定义DataGridViewColumn实现一个TreeViewColumn来在DataGridView控件中显示TreeView...

    上一篇博文探讨了如何自定义DataGridViewColumn实现一个TreeViewColumn来在DataGridView控件中显示TreeView控件,其实我们还可以继续发挥想象,自定义其他的列类型,下面介绍一个脚本编辑器列类型,我这里取名ScriptTextEditorColumn,当用户单击DataGridView的ScriptTextEditorColumn时,单元格右边会出现一个按钮,单击按钮会弹出一个脚本编辑器窗体,用户可以在窗体中进行代码维护,然后回写到单元格中。

  用人会问,这个控件有啥实际作用,其实结合动态编译的技术,在datagridview中进行取值公式的模板设定,也就是在对应的单元格中设置C#脚本,然后动态执行后呈现结果到一个datagridview单元格中,这样就实现了动态配置datagridview后台计算逻辑的目的,当然实现这样的功能还需要大量的工作,但是主要的思路就是这样。

1 ScriptTextEditorColumn

  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Windows.Forms;
  6  7namespace Host_Controls_in_Windows_Forms_DataGridView_Cells
  8{
  9publicclass ScriptTextEditorColumn : DataGridViewColumn
 10    {
 11public ScriptTextEditorColumn()
 12            : base(new ScriptTextEditorCell())
 13        {
 14        }
 15 16publicoverride DataGridViewCell CellTemplate
 17        {
 18get 19            {
 20returnbase.CellTemplate;
 21            }
 22set 23            {
 24// Ensure that the cell used for the template is a ScriptTextEditorCell. 25if (value != null &&
 26                     !value.GetType().IsAssignableFrom(typeof(ScriptTextEditorCell)))
 27                {
 28thrownew InvalidCastException("Must be a ScriptTextEditorCell");
 29                }
 30base.CellTemplate = value;
 31            }
 32        }
 33    }
 34 35//---------------------------------------------------------------------- 36publicclass  ScriptTextEditorCell : DataGridViewTextBoxCell
 37    {
 38 39public ScriptTextEditorCell()
 40             : base()
 41        {
 42 43        }
 44 45publicoverridevoid InitializeEditingControl(int rowIndex, object 46            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
 47        {
 48// Set the value of the editing control to the current cell value. 49base.InitializeEditingControl(rowIndex, initialFormattedValue,
 50                dataGridViewCellStyle);
 51             ScriptTextEditingControl ctl =
 52                 DataGridView.EditingControl as ScriptTextEditingControl;
 53// Use the default row value when Value property is null. 54if (this.Value == null)
 55            {
 56                 ctl.textBox1.Text = (String)this.DefaultNewRowValue;
 57            }
 58else 59            {
 60                 ctl.textBox1.Text = (String)this.Value;
 61            }
 62        }
 63 64publicoverride Type EditType
 65        {
 66get 67            {
 68// Return the type of the editing control that CalendarCell uses. 69returntypeof(ScriptTextEditingControl);
 70            }
 71        }
 72 73publicoverride Type ValueType
 74        {
 75get 76            {
 77// Return the type of the value that CalendarCell contains. 78 79returntypeof(String);
 80            }
 81        }
 82 83publicoverrideobject DefaultNewRowValue
 84        {
 85get 86            {
 87// Use the current date and time as the default value. 88string code = @" 89#region
 90//jackwangcumt
 91#endregion
 92using System;
 93using System.Collections.Generic;
 94using System.ComponentModel;
 95using System.Drawing;
 96using System.Data;
 97using System.Linq;
 98using System.Text;
 99using System.Windows.Forms;
100namespace Host_Controls_in_Windows_Forms_DataGridView_Cells
101{
102    public partial class SourceTextBox : UserControl
103    {
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        }
115116        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";
125return code;
126            }
127        }
128    }
129//-----------------------------------------------------------------130131class ScriptTextEditingControl : SourceTextBox, IDataGridViewEditingControl
132    {
133        DataGridView dataGridView;
134privatebool valueChanged = false;
135int rowIndex;
136137public ScriptTextEditingControl()
138        {
139//文本变更更新到cell140this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
141        }
142143void textBox1_TextChanged(object sender, EventArgs e)
144        {
145// Notify the DataGridView that the contents of the cell
146// have changed.147             valueChanged = true;
148this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
149//调用SourceTextBox的OnValueChanged(string Text)150base.OnValueChanged(this.textBox1.Text);
151        }
152153// Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
154// property.155publicobject EditingControlFormattedValue
156        {
157get158            {
159returnthis.textBox1.Text;
160            }
161set162            {
163if (value is String)
164                {
165try166                    {
167// This will throw an Exception of the string is 
168// null, empty, or not in the format of a date.169this.textBox1.Text=((String)value);
170                    }
171catch172                    {
173// In the case of an Exception, just use the 
174// default value so we're not left with a null
175// value.176this.textBox1.Text = "jackwangcumt>>error";
177                    }
178                }
179            }
180        }
181182// Implements the 
183// IDataGridViewEditingControl.GetEditingControlFormattedValue method.184publicobject GetEditingControlFormattedValue(
185            DataGridViewDataErrorContexts context)
186        {
187return EditingControlFormattedValue;
188        }
189190// Implements the 
191// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.192publicvoid ApplyCellStyleToEditingControl(
193            DataGridViewCellStyle dataGridViewCellStyle)
194        {
195this.Font = dataGridViewCellStyle.Font;
196//this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
197//this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;198        }
199200// Implements the IDataGridViewEditingControl.EditingControlRowIndex 
201// property.202publicint EditingControlRowIndex
203        {
204get205            {
206return rowIndex;
207            }
208set209            {
210                 rowIndex = value;
211            }
212        }
213214// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
215// method.216publicbool EditingControlWantsInputKey(
217             Keys key, bool dataGridViewWantsInputKey)
218        {
219// Let the DateTimePicker handle the keys listed.220switch (key & Keys.KeyCode)
221            {
222case Keys.Left:
223case Keys.Up:
224case Keys.Down:
225case Keys.Right:
226case Keys.Home:
227case Keys.End:
228case Keys.PageDown:
229case Keys.PageUp:
230returntrue;
231default:
232return !dataGridViewWantsInputKey;
233            }
234        }
235236// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
237// method.238publicvoid PrepareEditingControlForEdit(bool selectAll)
239        {
240// No preparation needs to be done.241        }
242243// Implements the IDataGridViewEditingControl
244// .RepositionEditingControlOnValueChange property.245publicbool RepositionEditingControlOnValueChange
246        {
247get248            {
249returnfalse;
250            }
251        }
252253// Implements the IDataGridViewEditingControl
254// .EditingControlDataGridView property.255public DataGridView EditingControlDataGridView
256        {
257get258            {
259return dataGridView;
260            }
261set262            {
263                 dataGridView = value;
264            }
265        }
266267// Implements the IDataGridViewEditingControl
268// .EditingControlValueChanged property.269publicbool EditingControlValueChanged
270        {
271get272            {
273return valueChanged;
274            }
275set276            {
277                 valueChanged = value;
278            }
279        }
280281// Implements the IDataGridViewEditingControl
282// .EditingPanelCursor property.283public Cursor EditingPanelCursor
284        {
285get286            {
287returnbase.Cursor;
288            }
289        }
290291protectedoverridevoid OnTextChanged(EventArgs e)
292        {
293// Notify the DataGridView that the contents of the cell
294// have changed.295             valueChanged = true;
296this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
297base.OnTextChanged(e);
298299        }
300301    }
302303304305 }

2 SourceTextBox

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Drawing;
 5using System.Data;
 6using System.Linq;
 7using System.Text;
 8using System.Windows.Forms;
 910namespace Host_Controls_in_Windows_Forms_DataGridView_Cells
11{
12publicpartialclass SourceTextBox : UserControl
13    {
14public SourceTextBox()
15        {
16            InitializeComponent();
17this.textBox1.Location = this.Location;
18this.textBox1.Width = this.Width;
19this.textBox1.Height = this.Height;
20        }
21protectedvoid OnValueChanged(string text)
22        {
23this.textBox1.Text = text;
24        }
2526privatevoid btnSource_Click(object sender, EventArgs e)
27        {
28             ScriptEditor frm = new ScriptEditor(this.textBox1.Text);
29            frm.ShowDialog();
30this.textBox1.Text = frm.fastColoredTextBox1.Text;
31        }
32    }
33 }

C#如何在DataGridViewCell中自定义脚本编辑器C#如何在DataGridViewCell中自定义脚本编辑器

 1namespace Host_Controls_in_Windows_Forms_DataGridView_Cells
 2{
 3partialclass SourceTextBox
 4    {
 5///<summary> 6/// 必需的设计器变量。
 7///</summary> 8private System.ComponentModel.IContainer components = null;
 910///<summary>11/// 清理所有正在使用的资源。
12///</summary>13///<param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>14protectedoverridevoid Dispose(bool disposing)
15        {
16if (disposing && (components != null))
17            {
18                components.Dispose();
19            }
20base.Dispose(disposing);
21        }
2223#region 组件设计器生成的代码
2425///<summary>26/// 设计器支持所需的方法 - 不要
27/// 使用代码编辑器修改此方法的内容。
28///</summary>29privatevoid InitializeComponent()
30        {
31this.textBox1 = new System.Windows.Forms.TextBox();
32this.btnSource = new System.Windows.Forms.Button();
33this.SuspendLayout();
34//35// textBox1
36//37this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
38this.textBox1.Location = new System.Drawing.Point(3, 3);
39this.textBox1.Margin = new System.Windows.Forms.Padding(4);
40this.textBox1.Multiline = true;
41this.textBox1.Name = "textBox1";
42this.textBox1.Size = new System.Drawing.Size(175, 21);
43this.textBox1.TabIndex = 1;
44//45// btnSource
46//47this.btnSource.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
48                         | System.Windows.Forms.AnchorStyles.Right)));
49this.btnSource.BackColor = System.Drawing.Color.Transparent;
50this.btnSource.BackgroundImage = global::Host_Controls_in_Windows_Forms_DataGridView_Cells.Resource.setting;
51this.btnSource.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
52this.btnSource.FlatAppearance.BorderColor = System.Drawing.Color.White;
53this.btnSource.FlatAppearance.BorderSize = 0;
54this.btnSource.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
55this.btnSource.Font = new System.Drawing.Font("新宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
56this.btnSource.Location = new System.Drawing.Point(159, -1);
57this.btnSource.Margin = new System.Windows.Forms.Padding(0);
58this.btnSource.Name = "btnSource";
59this.btnSource.Size = new System.Drawing.Size(19, 25);
60this.btnSource.TabIndex = 0;
61this.btnSource.UseVisualStyleBackColor = false;
62this.btnSource.Click += new System.EventHandler(this.btnSource_Click);
63//64// SourceTextBox
65//66this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
67this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
68this.Controls.Add(this.btnSource);
69this.Controls.Add(this.textBox1);
70this.Margin = new System.Windows.Forms.Padding(0);
71this.Name = "SourceTextBox";
72this.Size = new System.Drawing.Size(178, 26);
73this.ResumeLayout(false);
74this.PerformLayout();
7576        }
7778#endregion7980public System.Windows.Forms.Button btnSource;
81public System.Windows.Forms.TextBox textBox1;
82    }
83 }

View Code

3 效果

C#如何在DataGridViewCell中自定义脚本编辑器

相关图片

相关文章