Custom Editing Behavior for DataGridView TextBox Columns
来源:互联网 发布:网络视听许可证好办吗 编辑:程序博客网 时间:2024/05/16 07:42
I’m currently working on a hobby project where I’m displaying a list of files in a way similar to the “details” view of Windows Explorer. For various reasons I’m using a DataGridView instead of a ListView, and while configuring the DataGridView to look like a ListView wasn’t much of a problem, there’s one thing that got on my nerves, which is the behavior of textbox cells in edit mode: It is much too easy to leave the edit mode accidentally, simply by pressing the cursor keys at the wrong time. For example when the text caret is positioned behind the last character of the textbox cell content, and you press the right arrow key: the focus then moves to the next cell. There are certainly use cases for this behavior, but for my purposes I wanted the text caret to be “captured” inside the textbox in edit mode until you press Enter, Tab or Escape (or use the mouse to click another cell).
The nice thing about the DataGridView is that you can tweak it a lot by deriving from existing classes for cells, columns and editing controls, overriding certain methods. In my case I suspected that the DataGridViewTextBoxEditingControl (derived from TextBox) would contain special code to determine when the cell should leave edit mode. As I didn’t know what to search the documentation for (and I was too impatient to read it completely, to be honest), I fired up Lutz Roeder’s Reflector and took a look at the decompiled code of the class members. With the option “Show Inherited Members” switched off it took me only a couple of seconds to come across the code of the EditingControlWantsInputKey method which looked exactly like what I was expecting (lucky me: it was the 4th member in the list ;-).
So here are the steps to change the behavior of the editing control:
First derive a class from DataGridViewTextBoxEditingControl and override the EditingControlWantsInputKey method:
public class CustomDataGridViewTextBoxEditingControl : DataGridViewTextBoxEditingControl{ public override bool EditingControlWantsInputKey( Keys keyData, bool dataGridViewWantsInputKey ) { switch (keyData & Keys.KeyCode) { case Keys.Prior: case Keys.Next: case Keys.End: case Keys.Home: case Keys.Left: case Keys.Up: case Keys.Right: case Keys.Down: case Keys.Delete: return true; } return base.EditingControlWantsInputKey( keyData, dataGridViewWantsInputKey ); }}
Then derive a class from DataGridViewTextBoxCell and override the EditType property to use the customized editing control:
public class CustomDataGridViewTextBoxCell : DataGridViewTextBoxCell{ public override Type EditType { get { return typeof( CustomDataGridViewTextBoxEditingControl ); } }}
Finally derive a class from DataGridViewTextBoxColumn to be able to use the new cell type.
public class CustomDataGridViewTextBoxColumn : DataGridViewColumn{ public CustomDataGridViewTextBoxColumn() : base( new CustomDataGridViewTextBoxCell() ) { }}
You can now use the new column type in your code as a replacement of the stock DataGridViewTextBoxColumn (the new type appears in the drop down lists for column types in the designer dialogs of the DataGridView control).
A demo project is available here, showing a DataGridView with a normal DataGridViewTextBoxColumn and the CustomDataGridViewTextBoxColumn next to each other, so you can compare the different behaviors.
- Custom Editing Behavior for DataGridView TextBox Columns
- Custom an event for DataGridView.ContextMenuStrip
- Editing Creating Custom Listeners
- datagridview to textbox
- Test for Required Behavior, not Incidental Behavior
- Test for Required Behavior, Not Incidental Behavior
- DataGridView绑定数据到textbox
- C# dataGridView textbox 同步更新
- How to create readonly columns in MOSS 2007 custom list
- Defining Custom Columns in SharePoint – Field Types and Options
- Custom drawn DataGridView Cells with GDI+
- 如何启动plugin for editing C++ code
- linux shell bash for editing file
- [Editing] Deep Residual Learning for Image Recognition
- 文本框textbox如何同datagridview绑定
- Winfrom实现datagridview拖拽至TextBox(C#)
- dataGridView将数据显示到textBox
- datagridview数据拖到textbox中
- Android之理解Looper、Handler、Message、MessageQueue
- android 调试常用命令
- To make the android emulator (AVD) work fine for ADT V22.2.1
- Lync 2010升级到Lync 2013之Lync 2010外部用户登录设置及验证
- 适配器模式(Adapter Pattern)
- Custom Editing Behavior for DataGridView TextBox Columns
- grub2进入稳定分支
- js模块模式实例
- Redesign Your App for iOS 7 之 页面布局
- 不带缓存的文件I/O操作 read/write
- 一个自动将状态机生成代码的软件
- SharePoint 2013 Custom Calendar Colors
- Android之Looper、Handler、Message、MessageQueue应用篇
- ubuntu下解压文件乱码的解决方法