StrataFrame Forum

Error not shown in xtragrid for custom property

http://forum.strataframe.net/Topic8119.aspx

By Chan - 4/15/2007

Hi,

I have created custom property and bind it to xtragrid.column. Error provider for this property doesnt be shown when I save data. It works for other property (mapped to data)

Any ideas?

Thank you

By Trent L. Taylor - 4/16/2007

Do you have the BusienssFieldDIsplayInEditorField attribute assigned to the custom property?

<Browsable(False), _
     BusinessFieldDisplayInEditor(), _
     Description("My Custom Field"), _
     DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public property MyCustomField As String
   ....
End Property

By Chan - 4/16/2007

Hi,

Yes, I did

        [Description("Item Code"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        Browsable(false), MicroFour.StrataFrame.UI.Windows.Forms.BusinessFieldDisplayInEditor()]
        public System.String ItemCode
        {
            get
            {
                SqlCommand loCommand = new SqlCommand();
                loCommand.CommandText = "Select ItemCode FROM Items WHERE ItemID = @lcItemID";
                loCommand.Parameters.Add("@lcItemID", SqlDbType.UniqueIdentifier);
                loCommand.Parameters["@lcItemID"].Value = this.ItemID;
                return (System.String)this.ExecuteScalar(loCommand);
            }
            set
            {
                //
            }
        }
By Trent L. Taylor - 4/16/2007

More than likely your custom property is not set in error then.  You will have to set the column in error before it will show an error provider for that field. 
By Chan - 4/16/2007

Hi,

I have code in checkrulesoncurrentrow() as below. I debug the code and found it run properly. Just that the error provider not shown when save.

        private void ReturnNoteDetailsBO_CheckRulesOnCurrentRow(CheckRulesEventArgs e)
        {
            if (this.ItemCode == null || this.ItemCode.Length == 0)
            {
                this.AddBrokenRule("ItemCode", this.ErrorProviderRequiredFieldDesc);
            }
        }
By Trent L. Taylor - 4/16/2007

It is because the custom property is not set in error.  For example, when using ADO.NET, you can set the underlying column in error by calling the ADO.NET SetError method which will then interface with the error provider.  If you create an error provider, you can see how it interfaces with a property or a bound item when dealing with an error.  In this case, the actual bound field is not actually being set in error because there is not an underlying field in the ADO.NET table more than likely.
By Chan - 4/16/2007

Hi,

How to accomplish this? Sorry, still new to .NET.

Thank you

By StrataFrame Team - 4/17/2007

When the IDataErrorInfo interface returns the error for the specified column in the BusinessLayer class, it first checks the BrokenRules collection to see if there is a broken rule for the specified field, then it checks the underlying DataTable for errors on the column.  What you need to do is put a break point in your CheckRulesOnCurrentRow() method and check the value of ((System.ComponentModel.IDataErrorInfo)this)["ItemCode"]  If the value of this is an empty string, then the implementation of the IDataErrorInfo.[this] property is not returning properly; however, if it returns a value, then the grid is not properly displaying the error provider.  Let me know which one it is.
By Chan - 4/17/2007

Hi,

((System.ComponentModel.IDataErrorInfo)this)["ItemCode"] return empty string.

By StrataFrame Team - 4/17/2007

Here's the code for the IDataErrorInfo.Item get:

                '-- Return the error for the given field
                If Me.BrokenRules.Contains(Me.CurrentRowIndex, columnName) Then
                    Return Me.BrokenRules(Me.CurrentRowIndex, columnName).Description
                ElseIf Me.CurrentDataTable.Columns.Contains(columnName) Then
                    Return Me.CurrentRow.GetColumnError(columnName)
                Else
                    Return String.Empty
                End If

So, if the BrokenRules collection has an entry for the current row and "ItemCode", then it will return the description in the broken rule.  Otherwise, it checks the underlying DataTable for errors, then just returns an empty string.  Since your custom field does not exist as a column, the second condition will never be hit, so you have to verify that the broken rule is being added properly.

You can use the ToArray() method on the BrokenRules to see all of the rules in the collection and make sure that you're rule is in there for the "ItemCode" property.

By Chan - 8/11/2007

Hi,

How could I make control response to IDataErrorInfo for custom field?



Thank you
By Trent L. Taylor - 8/13/2007

It will work if your custom field has a field within the DataTable in the BO.  If you have a custom property that does not pull from the internal data table of the BO then it will not return an error.  The same applies for the broken rule.  So at present, without having another object, you would have to add the field to the SELECT or manually to the data table using the Columns collection of the CurrentDatatable.  Once the field exists, then you can just add a broken rule:

Me.AddBrokenRule("MyCustomField", "My Error")