This is a migrated thread and some comments may be shown as answers.

INotifyDataErrorInfo for RadGridView

2 Answers 297 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mehri
Top achievements
Rank 1
Mehri asked on 10 Jul 2011, 07:04 AM
Hi Support

I implement INotifyDataErrorInfo for my class ,It shows exception right in GridView But allow user to exit cell with invalid data.
I want to force user to correct data before leaving cell how can I do that?

public

class TestClass : INotifyDataErrorInfo  

{

        int code; 
                public int Code  

                {

                    get {return code; }

                    set 
                    {
            IsCodeValid(
value); 

            if (code != value) code = value;  

                    }

                }

        string name;

        public string Name  

                {

                    get {return name; }

                    set {name = value; }

                }

public bool IsCodeValid(int value)  

{

bool isValid = true;

if (value == 0)

{

AddError("Code", "Code Err!", false);

isValid = false;  

}

else RemoveError("Code", "Code Err!");

return isValid;

}
private
Dictionary<String, List<String>> errors = new Dictionary<string, List<string>>();

public void AddError(string propertyName, string error, bool isWarning)

{

if (!errors.ContainsKey(propertyName))  

errors[propertyName] = new List<string>();

if (!errors[propertyName].Contains(error))

{

if (isWarning) errors[propertyName].Add(error);

else errors[propertyName].Insert(0, error);

RaiseErrorsChanged(propertyName);

}

}

public void RemoveError(string propertyName, string error)  

{

if (errors.ContainsKey(propertyName) && errors[propertyName].Contains(error))

{

errors[propertyName].Remove(error);

if (errors[propertyName].Count == 0) errors.Remove(propertyName);

RaiseErrorsChanged(propertyName);

}

}

public void RaiseErrorsChanged(string propertyName)

{

if (ErrorsChanged != null)

ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));

}

#region

INotifyDataErrorInfo Members

public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

public System.Collections.IEnumerable GetErrors(string propertyName)

{

if (String.IsNullOrEmpty(propertyName) || !errors.ContainsKey(propertyName))

return null;

return errors[propertyName];

}

public bool HasErrors

{

get { return errors.Count > 0; }

}

#endregion

 

 

}

 

 

<telerik:RadGridView Name="radGridView1" AutoGenerateColumns="False" ItemsSource="{Binding ColData}" >

 

<telerik:RadGridView.Columns>

 

<telerik:GridViewDataColumn Header="Code" DataMemberBinding="{Binding Code}" ValidatesOnDataErrors="Default" />

 

<telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" />

 

</telerik:RadGridView.Columns>

 

</telerik:RadGridView>

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 11 Jul 2011, 08:42 AM
Hi Mehri,

 Have you tried setting the ActionOnLostFocus property of the RadGridView to Commit Edit. This will force the GridView to validate the cell content. 

Is this what you are looking for?

All the best,
Didie
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Mehri
Top achievements
Rank 1
answered on 12 Jul 2011, 09:37 AM
Hi Support
Thanks for your reply, it solved our problem.

All the best
Mehri
Tags
GridView
Asked by
Mehri
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Mehri
Top achievements
Rank 1
Share this question
or