DataGridView in TabControl and CellValidating lead to problems
I created a little form with a TabControl on it and a combobox.
On the first page i added a DataGridView with 2 columns, not bound to any data (data entered directly).
Also, for the grid i added an event handler for CellValidating in which i test if the data from the first row is numeric, if not i show a message box with an error and return e.Cancel = true so the DataGridView maintains focus.
Now, if i do the following :
1 - edit cell on first column and enter a wrong value (a text)
2 - click on TabControl to change to the other tab page, error is shown that the value is not correct, close the error
3 - modify the value to be correct (enter a number)
4 - change to any of the other cells in the DataGridView and try to edit them with the mouse (double click to edit), it just doesn't work.
The only solution to "fix" this is to change to the other tabPage on the TabControl and back, this way the DataGridView regains its ability to edit cells with the mouse.
Is there any solution to this ?
Note: If i don't show the message box in the CellValidating function then the problem doesn't occur. I know i could show an errormessage on the row of the column, but the application i'm writing requires that i display messageboxes for all errors (and i can't make the grid work differently from all other controls on the other dialogs just because of this bug).
So, is there any way to fix this ?
Here's my code (at least the part that relates to the problem)
public Form1()
{
InitializeComponent(); // add some data to the grid so it's not empty // first row
DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell value = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell text = new DataGridViewTextBoxCell();
value.Value = "12";
text.Value = "some text"; row.Cells.Add(value);
row.Cells.Add(text); dataGridView1.Rows.Add(row); // second row
row = new DataGridViewRow();
value = new DataGridViewTextBoxCell();
text = new DataGridViewTextBoxCell();
value.Value = "1000";
text.Value = "another text"; row.Cells.Add(value);
row.Cells.Add(text); dataGridView1.Rows.Add(row);
} private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// first row must be numeric value
if (e.ColumnIndex == 0)
{
try
{
double x = Convert.ToDouble(e.FormattedValue);
}
catch (System.Exception ex)
{
MessageBox.Show("Error, value is not a double");
// if we can't convert then bail out
e.Cancel = true;
}
}
} ...
private void InitializeComponent()
{
...
this.dataGridView1.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dataGridView1_CellValidating);
...
}
该问题源自MSDN Bug
http://social.msdn.microsoft.com/Forums/windows/en-US/c3634471-49cb-4274-afa4-c4bcd184b52c/datagridview-in-tabcontrol-and-cellvalidating-lead-to-problems
如果觉得英文不爽,这有这个问题的中文版
http://bbs.csdn.net/topics/390492161?page=1#post-394804656
下面是我对这个问题的规避方法
首先确定问题是在切换页签引发的校验中,弹出弹窗,导致焦点失去,从而致使datagridview Cell无法再进入编辑模式。
所以可以通过在校验过程弹窗后,重新fouce。
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{if(e.ColumnIndex==-||e.RowIndex==-)
{
return;
}
if(string.IsNullOrEmpty(e.FormattedValue.ToString()))
{
return;
}
int tempValue = -;
if(!int.TryParse(e.FormattedValue.ToString(),out tempValue))
{
MessageBox.Show("Erro Data.");
dataGridView1.Focus();
e.Cancel = true;
dataGridView1.CancelEdit();
}
}
但是fouce后会导致tab页签发生切换,而不是我们想要的留在该页签(这里是因为焦点重获得,导致某个标志位值被修改,tabControl未进入留在本页签的代码分支),所以需要为tabControl写Deselecting事件来“要求停留在本页签”,这里需要一个标志位,来实现如果校验通过,那么页签正常切换,如果失败不能切换。
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
m_IsInvalid = false;
if(e.ColumnIndex==-||e.RowIndex==-)
{
return;
}
if(string.IsNullOrEmpty(e.FormattedValue.ToString()))
{
return;
}
int tempValue = -;
if(!int.TryParse(e.FormattedValue.ToString(),out tempValue))
{
MessageBox.Show("Erro Data.");
dataGridView1.Focus();
e.Cancel = true;
m_IsInvalid = true;
dataGridView1.CancelEdit();
}
}
private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage == tabPage2)
{
if (m_IsInvalid)
{
e.Cancel = true;
}
}
}
private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
m_IsInvalid = false;
if (e.ColumnIndex == - || e.RowIndex == -)
{
return;
}
MessageBox.Show("Erro Data.");
dataGridView1.Focus();
e.Cancel = true;
m_IsInvalid = true;
dataGridView1.CancelEdit();
}
这样就规避了页签切换校验失败,导致无法点击鼠标编辑datagridview cell的问题。
当然,坑一般是连着的,datagridview的CausesValidation一旦设为true,那么就是一旦焦点失去,它就触发校验,这样用户体验感非常差
想想看,我表格填错了,想直接点cancel取消掉,都要各种弹窗,无法正常Cancel。所以我们一般做了这样的策略
就是如果是在表格内进行操作,那么焦点失去立即校验。如果是在表格外,那么取消这一坑爹设定。
private void dataGridView1_MouseLeave(object sender, EventArgs e)
{
dataGridView1.CausesValidation = false;
} private void dataGridView1_MouseEnter(object sender, EventArgs e)
{
dataGridView1.CausesValidation = true;
}
好,这样,我们的Cancel,关闭窗体的X,就能正常运作了。
但是,但是。。。。我们发现页签切换点击,也是在表格外,这样就会导致页签切换时,触发一个行校验后,再点击页签切换,就不会触发表格校验,同时因为我们之前设置的全局变量导致页签一直无法切换,也没有提示。
我们必须在点击页签切换时,将dataGridView1.CausesValidation从false重新赋值为true。很悲剧的是,这样的事件微软没有给出。所以必须要重写下tabControl控件
public class CustomTabControl:TabControl
{
public event EventHandler<CancelEventArgs> PreClicked; protected override void WndProc(ref Message m)
{
if (m.Msg == )
{
var cancelArg = new CancelEventArgs(false);
OnPreClicked(cancelArg);
if (cancelArg.Cancel)
{
return;
}
}
base.WndProc(ref m);
}
private void OnPreClicked(CancelEventArgs e)
{
var handler = PreClicked;
if(handler!=null)
{
handler(this,e );
}
}
}
Form1窗体:
private void tabControl1_PreClicked(object sender, CancelEventArgs e)
{
dataGridView1.CausesValidation = true;
}
这样所有就能按照设计正常运行了。
DataGridView in TabControl and CellValidating lead to problems的更多相关文章
- The `XXXX` target overrides the `HEADER_SEARCH_PATHS` build setting defined in `Pods/Target Support Files/Pods-game-desktop/Pods-game-desktop.release.xcconfig'. This can lead to prob
The `game-desktop [Release]` target overrides the `HEADER_SEARCH_PATHS` build setting defined in `Po ...
- DataGridView使用
DataGridView控件概述 DataGridView 控件代码目录(Windows 窗体) 未绑定数据列 定义:可能想要显示并非来自数据源的一列数据,这种列称为未绑定列. 数据格式示例 如何:设 ...
- Android Lint Checks
Android Lint Checks Here are the current list of checks that lint performs as of Android Studio 2.3 ...
- iOS cocoapods升级及问题
安装 安装RubyCocoaPods基于Ruby语言开发而成,因此安装CocoaPods前需要安装Ruby环境.幸运的是Mac系统默认自带Ruby环境,如果没有请自行查找安装.检测是否安装Ruby:$ ...
- CocoaPods的使用及安装
本文转自:http://www.jianshu.com/p/6e5c0f78200a 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发i ...
- android 官方文档 JNI TIPS
文章地址 http://developer.android.com/training/articles/perf-jni.html JNI Tips JNI is the Java Native I ...
- iOS 如何在一个已经存在多个project的workspace中引入cocoapods管理第三方类库
一种新的第三方库管理工具:Carthage 如何使用Carthage管理iOS依赖库 Podfile Syntax Reference v1.1.0.rc.3 https://guides.cocoa ...
- cocoapod集成失败,无法找到头文件的解决办法
在终端更新pod的时候,提示警告: target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support ...
- ubuntu-利用pdnsd-TCP方式获取IP-拒绝DNS污染
那,自从国内技术出现了DNS污染问题呢,时常导致很多国外网站访问不正常,所以通过参考一些博客所属避免DNS污染的方法,决定搭建一个Ubuntu JeOS下的DNS缓存服务器,该服务器利用TCP方式获取 ...
随机推荐
- 传输层(2)-TCP连接的建立和终止、TIME_WAIT状态
1.TCP连接的建立和终止 1)三路握手 客户端发送一个SYN(同步)分解,告诉服务器客户将在连接中发送的数据的初始序列号. 服务器发送确认客户的SYN(ACK),同时自己也得发送一个SYN分节,它含 ...
- Slyx_SerAddGet
##通道##119.29.192.206:12002## ##通道##58.221.49.24:12002##
- [lucene系列笔记1]lucene6的安装与配置(Windows系统)
lucene是一个java开源的高效全文检索工具包,最近做项目要用到,把学习的过程记录一下. 第一步:下载安装jdk 1.首先从官网下载jdk(下载之前先查看你的电脑是多少位操作系统,如果是32就下载 ...
- scala操作符&运行
---恢复内容开始--- #操作符运算 数学操作符 + - * / % 比较操作符 < > >= =< 逻辑操作符 && || 对等操作符 == != 位操 ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 售价249英镑 我曾经花了 ...
- Android中的事件传递机制
Android源码版本:API Level 19(Android 4.4) Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和 ...
- 反射——Java反射机制
反射概述 什么是反射? ① 反射的概念是由Smith在1982年首次提出的,主要指程序可以访问.检测和修改它本身状态或行为的一种能力. ② JAVA反射机制是在运行状态中,对应任意一个类,都能 ...
- struts2配置文件中Action中的各属性的含义
StrutsApacheBeanJSPServlet attribute: 这个属性用来指定ActionForm保存到指定上下文时所使用的属性名.如果不指定attribute属性的值,将使用 ...
- logback logback.xml常用配置详解(三) <filter>
<filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY,日志将立即被抛弃不再经过其他过滤器:返回NEUTRAL,有序列表 ...
- modelsim搭建uvm环境及实例
Modelsim SE-64 10.2c 自带编译好的uvm-1.1d 脚本new_run.do set UVM_DPI_HOME C:/modeltech64_10.2c/uvm-1.1d/win6 ...