.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share

 Subscribe to Articles

Validate data in DataGrid in silverlight Application

Posted By :Diptimaya Patra      Posted Date :16/03/2010   Points :25   Category: Silverlight    URL: http://dpatra.blogspot.com

Validate data in DataGrid in silverlight Application. In this article we will see how it can be implemented to a DataGrid. That means validating user input in DataGrid.
 


Introduction

We have already seen how to validate input data using DataAnnotations and DataInput controls. In this article we will see how it can be implemented to a DataGrid. That means validating user input in DataGrid.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as DataValidationDataGridSL3.

1.gif

Now we need a DataGrid to display sample data.

Go ahead and add it and name it as MyDataGrid.

2.gif

Now we need to add a class which contains the properties of the sample data.

public class Users
{
  public string Name { get; set; }
  public int Age { get; set; }
  public string Gender { get; set; }
  public string Country { get; set; }
}


DataAnnotation provides a developer friendly DataValidation techniques. So we need to add the assembly.

4.gif

The ValidationException (belonging to System.ComponentModel.DataAnnotations ) thrown is displayed as Cell Validation Error.

Now that you have added the above assembly, use it in the Users.cs class and define the properties as required. As follows:

public class Users: INotifyPropertyChanged
  {
  #region INotifyPropertyChanged Members
  public event PropertyChangedEventHandler PropertyChanged;
  #endregion 
  #region UserName 
  private string _UserName; 
  public string UserName 
  { 
  get { return _UserName; } 
  set 
  { 
  if (value.Length < 4) 
   { 
  throw new ValidationException("User Name should contain atleast 4 chars"); 
  } 
  _UserName = value; 
  RaisePropertyChanged("UserName"); 
  } 
  } 
  #endregion 
 

  #region Age 
  private int _Age; 
  public int Age 
  { 
  get { return _Age; } 
  set 
  { 
  _Age = value; 
  } 
  } 
  #endregion 
 

  #region Gender 
  private string _Gender; 
  public string Gender 
  { 
  get { return _Gender; } 
  set 
  { 
  _Gender = value; 
  } 
  } 
  #endregion 
 

  #region Country 
  private string _Country; 
  public string Country 
  { 
  get { return _Country; } 
  set 
  { 
  _Country = value; 
  } 
  } 
  #endregion  

  private void RaisePropertyChanged(string propertyName) 
  { 
   if (this.PropertyChanged != null) 
  { 
  this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
  } 
  } 
  }


Now add some sample data in the MainPage constructor and bind the DataGrid's ItemSource to the sample data.

public MainPage()
  {
  InitializeComponent();  
  List<Users> myList = new List<Users> 
  { 
  new Users{ UserName="Hiro Nakamura", Age=24, Gender="M", Country="Japan"}, 
  new Users{ UserName="Mohinder Suresh", Age=26, Gender="M", Country="India"}, 
  new Users{ UserName="Claire Bennette", Age=20, Gender="F", Country="USA"}, 
  new Users{ UserName="Matt Parkman", Age=30, Gender="M", Country="USA"}, 
  new Users{ UserName="Nathan Patrelli", Age=30, Gender="M", Country="USA"}, 
  new Users{ UserName="Peter Patrelli", Age=26, Gender="M", Country="USA"}, 
  new Users{ UserName="Mica Sanders", Age=19, Gender="M", Country="USA"}, 
  new Users{ UserName="Linderman", Age=56, Gender="M", Country="USA"}, 
  new Users{ UserName="Ando", Age=24, Gender="M", Country="Japan"}, 
  new Users{ UserName="Maya", Age=24, Gender="F", Country="Mexico"}, 
  new Users{ UserName="Angela Patrelli", Age=26, Gender="F", Country="USA"}, 
  new Users{ UserName="Niki Sanders", Age=26, Gender="F", Country="USA"}, 
  }; 

  MyDataGrid.ItemsSource = myList; 
  }

That's it run your application and try to change the first name and keep it empty or enter less than 4 characters. It will throw you a ValidationException. As follows:

3.gif

Try for other columns as required.

Enjoy Coding.


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend