Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am working on a silverlight project, where i am using a datagrid to display some data. User can also update the data using this grid. One column in this grid should accept only numeric values. How can i perform client side validation for this. Any idea. . . ?
Posted
Comments
Christian Amado 26-Jul-12 11:45am    
The second alternative must be improved, it's only for reference. :)

1 solution

If you are using WCF RIA Services to fill the DataGrid you can implement DataAnnotations in your DomainService metadata file. Eg.Using Data Annotations to Customize Data Classes.
For your case you can Use RegularExpression Attribute to allow only numeric values for a Column. In this case you can controls data via WCF asynchronously, obviously on client side.

The following alternative don't like me, but...
If you don't use WCF RIA Services you can call a TextInput Event for DataGrid:
XML
<usercontrol x:class="DataGridExample.MainPage" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <grid x:name="LayoutRoot" background="White">
        <controls:datagrid x:name="gridData" textinput="gridData_TextInput" xmlns:controls="#unknown" />
    </grid>
</usercontrol>


And the code behind is:
C#
private void gridData_TextInput(object sender, TextCompositionEventArgs e)
        {
            string value = e.Text;

            //Verify if value is numeric or not
            decimal test = -1;
            decimal.TryParse(value, out test);

            if (test == -1)//if value of test is -1, is not numeric so            
                e.Handled = true;
        }

Hope it helps. :D
 
Share this answer
 
Comments
Sandeep Mewara 26-Jul-12 12:46pm    
My 51 Good answer.
Christian Amado 26-Jul-12 13:20pm    
Nice to help you. Can you mark this as solution? Just to help another user =)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900