如何在UltraGrid中单击取消按钮时停止单元格验证

时间:2017-08-23 20:10:41

标签: infragistics ultragrid ultrawingrid

在我的ultragrid中,我有一个Nullable = Disallow的列,这意味着该字段不能为空。如果我尝试将列编辑为空字符串,则会引发我的CellDataError事件,正如我所期望的那样。然而,我想在整个对话框中点击取消(单元格仍为空白),这会再次触发验证。

如何在点击取消按钮时跳过验证?

1 个答案:

答案 0 :(得分:0)

为了跳过验证,您必须在单击取消按钮时允许可为空的值,然后在更新单元格时再次禁用可空值。以下是使用代码实现它的方法。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.ultraGrid1.DataSource = InitializeGridSource();
        this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Nullable = Infragistics.Win.UltraWinGrid.Nullable.Disallow;
        this.ultraGrid1.AfterCellUpdate += new CellEventHandler(ultraGrid1_AfterCellUpdate);
        this.Deactivate += new EventHandler(Form1_Deactivate);
    }

    private void ultraGrid1_AfterCellUpdate(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
    {
        this.ultraGrid1.ActiveCell.Column.Nullable = Infragistics.Win.UltraWinGrid.Nullable.Disallow;
    }

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        if (this.OwnedForms.Length > 0 && this.OwnedForms[0].Text == "Data Error")
        {
            this.OwnedForms[0].FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if ((sender as Form).DialogResult == System.Windows.Forms.DialogResult.Cancel)
        {
            var activeCell = this.ultraGrid1.ActiveCell;
            activeCell.Column.Nullable = Infragistics.Win.UltraWinGrid.Nullable.Automatic;
            activeCell.EditorResolved.ExitEditMode(forceExit: true, applyChanges: true);
            this.ultraGrid1.UpdateData();     
        }
    }

    private DataTable InitializeGridSource(int rows = 7)
    {
        DataTable newTable = new DataTable("Table1");

        newTable.Columns.Add("String Column", typeof(string));

        for (int index = 0; index < rows; index++)
        {
            newTable.Rows.Add(new object[] { "Text " + index });
        }

        return newTable;
    }
}