DevExpress GridControl中的验证单元格

问题描述 投票:3回答:3

只是一个简单的问题,我无法管理自己。

我有一个WinForms(12.2)的DevExpress GridControl,其中填充了一些数值,该网格是可编辑的,用户可以更改这些值。

想象用户改变了一个,我想要的是验证此单元格,以便在我的数据源中修改相应的值,而无需单击单元格外部。

也就是说,我希望用户仅通过按工具栏上的按钮就可以验证并应用所有值,而不是单击enter,esc或在表中单击。

我正在寻找一些论坛,但没有得到正确的答案

谢谢,

validation devexpress datasource gridcontrol
3个回答
1
投票

在您对menuItem_click的处理程序中,执行以下操作:

private menuItem_Click(object sender, EventArgs e)
{
  gridView1.UpdateCurrentRow(); //return a bool, false = if validation error(s) was found
}

这将强制视图验证输入并将其下推到数据源。


2
投票

取决于您要做什么。您有2个选择。验证行并返回显示错误消息的消息框。或者您也可以在单元格中放入红色的小“ 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


0
投票

接受的答案,UpdateCurrentRow()完全按照其说的做,它强制视图更新其结果,如果由于验证错误而无法返回结果,则返回false。但这还不是全部。

要引起验证错误,您需要使用ValidateRow或ValidatingEditor。因此这些一起使用。

不同之处在于,在字段之间移动时,ValidatingEditor起作用。

此示例取自https://docs.devexpress.com/WindowsForms/3055/controls-and-libraries/data-grid/examples/data-editing/how-to-validate-data-entered-by-end-users

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.