首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# GridDataView Cell验证有关问题

2013-09-23 
C# GridDataView Cell验证问题有一个GridDataView,其Cell允许手动输入数据,对该数据要进行有效性验证.在Gr

C# GridDataView Cell验证问题
有一个GridDataView,其Cell允许手动输入数据,对该数据要进行有效性验证.

在GridDataView所在界面有几个按钮,Delete,Reset,OK,Cancel.

现在的问题是在输入数据尚未改变CurrentCell进行验证时,点击上述几个Button.
希望实现以下效果:
1.如果点击Delete,可以删除CurrentCell所在行,不管数据是否有效.
2.如果点击Reset,可以清空GridDataView,不管数据是否有效.
3.如果点击OK,则验证输入数据,如果有效则记录GirdDataView中数据,如果无效则提示数据无效,且不记录数据.
4.如果点击Cancel,则可以设置GridDataView.Enable属性,不管数据是否有效.


上述Button有可能是ToolStripButton,个人在调试中发现,gridSite_CellValidating有时在点击Button时发生,有时在Button中设置CurrentCell=null时发生,这个是最让人恼火的......


求解应如果搞定.
[解决办法]
把验证方法放到OK处理程序里
[解决办法]
CellValidating是那样,不过你可以判断啊,判断e.FormattedValue和cell的value是否一致,如果一致,就e.Cancel = true,标示没有进行修改
[解决办法]
reset和 delete 设置属性 CausesValidation = false
[解决办法]

引用
Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus.

看msdn例子:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx
[解决办法]
验证和你的按钮没关系。Validating的时候去处理
[解决办法]


简单和自定义数据验证

简单数据验证

            this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);


   private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.ThrowException = false;

        }

自定义数据验证
DataGridView.CellValidating 事件,在单元格失去输入焦点时发生,并启用内容验证功能。



        private void dataGridView1_CellValidating(object sender,    DataGridViewCellValidatingEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].ErrorText = "";
            int newInteger;

            // Don't try to validate the 'new row' until finished 
            // editing since there
            // is not any point in validating its initial value.
            if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
            if (!int.TryParse(e.FormattedValue.ToString(),
                out newInteger) 
[解决办法]
 newInteger < 0)
            {
                e.Cancel = true;
                dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
            }
        }

DataGridView.VirtualMode 属性
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.virtualmode(v=vs.80).aspx



[解决办法]
把验证移到DataGridView.RowValidating,单个cell出错可以不用管
[解决办法]
引用:
reset和 delete 设置属性 CausesValidation = false

6楼的方法是对的。不知道楼主怎么做的。
在dataGridView1_CellValidating事件中验证数据合法性,如果非法e.Cancel = true;
然后设定reset和 delete 设置属性 CausesValidation = false。这样点击reset和 delete是不会进入dataGridView1_CellValidating事件的。点击reset清空所有数据,点击delete删除当前行都试过没有问题。

热点排行