3

I am using DevExpress GridControl to create a form that enable user to do some data entry. The grid is bound to a DataTable with one column defined as unique as its DataSource.

DataTable dtDetail = new DataTable();
dtDetail.Columns.Add("ProductID", typeof(int));
dtDetail.Columns.Add("ProductName", typeof(string));
dtDetail.Columns.Add("OrderQty", typeof(int));
dtDetail.Columns.Add("LossTolerance", typeof(decimal));

dtDetail.Columns["ProductID"].Unique = true;

gridView.DataSource = dt;

Normally we can add rows and handle constraint violation with this code:

try
{    
    dtDetail.Rows.Add(1, "Some product name", 10, 2);
}
catch (ConstraintException ex)
{
    // The default ex.Message is something like: Column 'ProductID' is constrained to be unique. Value xxxx is already present. 
    // I need to display this message in my local language.
}

The form I designed is a data entry form, so the data comes from end user through the grid. When row is added by user, the dtDetail.Rows.Add method is called in some way and I don't know how to handle the ConstraintException properly, since the added row is coming from grid, not directly from my code.

When duplicate ProductID is about to added, a MessageBox will appear with exact text in ex.Message, with two YesNo buttons, asking us whether we want to correct the values or not.

My goal is to keep all rows within the DataTable is unique. I need to handle the ConstraintException so I can display custom error message to end user. I have searched some post on SO and Microsoft documentation like these posts:

but it doesn't give me clue on how to do that.

Could anyone please assist me how to do that? Actually, my code is running properly, I just need to handle the exception. Sorry for my bad English. English is not my native language. Thank you.

Hermanto
  • 552
  • 6
  • 15
  • Do the exception isnt thrown in the context of the Row.Add fuction, so maybe it is thrown on update or something like that. Try to make a new class based on the `GridView` and override methods that could throw the error and warp them with a try-catch statement. Once you figured it out please also tell us there the exception is thrown :) – Prophet Lamb Sep 24 '19 at 05:47
  • 1
    Did you try ValidateRow Event of GridControl ? – Oleg Sep 24 '19 at 05:47
  • @Oleg, I do use ValidateRow event but for different purpose (checking input values). But thank you for pointing me to this. I'm able to solve my requirement using InvalidRowException which will be raised if a row fails validation or caoont be saved to datasource, as stated in [link](https://documentation.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Base.ColumnView.InvalidRowException.event) – Hermanto Sep 24 '19 at 08:11

1 Answers1

0

As Oleg commented on comments section to use ValidateRow event, I'm able to solve my requirement using another event that actually related to that event, which is InvalidRowException

private void gridView_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
{
    // Get the type of exception
    if (e.Exception.GetType() == typeof(ConstraintException))
    {
        // Get the unique constraint column
        using (DataColumn constraintColumn = ((UniqueConstraint)dtDetail.Constraints[0]).Columns[0])
        {
            // Get the value that violates unique constraint
            object value = ((DataRowView)e.Row).Row[constraintColumn];

            DialogResult dr = XtraMessageBox.Show(string.Format("Kolom {0} diatur sebagai Unique. Nilai {1} telah ada sebelumnya. Apakah Anda ingin memperbaiki barisnya?", constraintColumn.ColumnName, value.ToString()), "Informasi", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (dr == DialogResult.Yes)
            {
                // No action. User can correct their input.
                e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            }
            else
            {
                // Duplicate row will be removed
                e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.Ignore;
            }
        }
    }
}

By using above code, I can handle the ConstraintException and display custom error message for my user.

Thank you.

Hermanto
  • 552
  • 6
  • 15