这是一个我自己无法解决的简单问题。
我有一个DevExpress GridControl for Winforms(12.2),里面填充了一些数字值,该表格可编辑,用户可以更改这些值。
想象一下,如果用户更改了其中一个值,我希望验证此单元格,以便在不点击单元格外部的情况下获取相应的修改后的数据源中的值。
也就是说,我希望用户能够通过在工具栏中按一个按钮来验证和应用所有值,而不是点击Enter、Esc或者点击表格。
我在一些论坛上搜索,但没有得到正确的答案。
谢谢。
这是一个我自己无法解决的简单问题。
我有一个DevExpress GridControl for Winforms(12.2),里面填充了一些数字值,该表格可编辑,用户可以更改这些值。
想象一下,如果用户更改了其中一个值,我希望验证此单元格,以便在不点击单元格外部的情况下获取相应的修改后的数据源中的值。
也就是说,我希望用户能够通过在工具栏中按一个按钮来验证和应用所有值,而不是点击Enter、Esc或者点击表格。
我在一些论坛上搜索,但没有得到正确的答案。
谢谢。
这要看你想做什么。你有两个选项。要么验证行并返回一个显示错误消息的消息框,要么在单元格内放置小红色“x”。
这两种方法都可以用,但需要略微不同的实现。这两种方法都需要你订阅gridview的validate row事件,而不是gridcontrol。
像这样的代码会给你一个文本框:
private void gridView1_ValidateRow(object sender,
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
{
e.Valid = false;
}
类似这样的内容会在单元格中显示红色的“X”:
private void gridView1_ValidateRow(object sender,
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
GridView view = sender as GridView;
GridColumn inStockCol = view.Columns["UnitsInStock"];
GridColumn onOrderCol = view.Columns["UnitsOnOrder"];
//Get the value of the first column
Int16 inSt = (Int16)view.GetRowCellValue(e.RowHandle, inStockCol);
//Get the value of the second column
Int16 onOrd = (Int16)view.GetRowCellValue(e.RowHandle, onOrderCol);
//Validity criterion
if (inSt < onOrd) {
//Set errors with specific descriptions for the columns
view.SetColumnError(inStockCol, "The value must be greater than Units On Order");
view.SetColumnError(onOrderCol, "The value must be less than Units In Stock");
}
}
这些信息可以在此处找到:http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsBaseColumnView_ValidateRowtopic
但是这仍然需要用户退出单元格,
我在这里找到了更多信息:http://www.devexpress.com/Support/Center/p/A289.aspx
private menuItem_Click(object sender, EventArgs e)
{
gridView1.UpdateCurrentRow(); //return a bool, false = if validation error(s) was found
}
被接受的答案,UpdateCurrentRow()恰好做了它所说的 - 它强制视图更新其结果,如果由于验证错误而无法更新,则返回false。但这并不是全部。
要引起验证错误,您需要使用ValidateRow或ValidatingEditor。因此,它们一起使用。
区别在于ValidatingEditor在字段之间移动时起作用。
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
ColumnView view = sender as ColumnView;
GridColumn column = (e as EditFormValidateEditorEventArgs)?.Column ?? view.FocusedColumn;
if (column.Name != "colBudget") return;
if ((Convert.ToInt32(e.Value) < 0) || (Convert.ToInt32(e.Value) > 1000000))
e.Valid = false;
}
private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
ColumnView view = sender as ColumnView;
if (view == null) return;
e.ExceptionMode = ExceptionMode.DisplayError;
e.WindowCaption = "Input Error";
e.ErrorText = "The value should be greater than 0 and less than 1,000,000";
// Destroy the editor and discard the changes made within the edited cell.
view.HideEditor();
}
我的代码通常长这样(VB):
Private Function ValidateView(view As ColumnView) As Boolean
If view.IsEditing Then
view.CloseEditor()
Return view.UpdateCurrentRow()
End If
Return True
End Function