Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to validate keypress in gridview

for ex i have column named no.of days in gridView there i have to give only numbers upto certain limit..how can i validate this by using keypress event
Posted

1 solution

Have you tried adding regex validators or custom validators in the gridview. Even Javascript can help you achieve your requirement. See below:


JavaScript
<script type="text/javascript" language=javascript>
function keyDownNumber()
{
    var key;
    if(navigator.appName == 'Microsoft Internet Explorer')
        key = event.keyCode;
    else
        key = event.which

    if ( !(key >= 48 && key <= 57) && key != 8 && key != 46 && key != 36 && key != 37)
    {
        event.returnValue = false;
    }
}
</script>


ASP.NET
<Columns>
<asp:templatefield headertext="Days">
        <itemtemplate>
          <asp:TextBox id="Days" text='<% # eval("Days") %>' runat="server"
            onkeypress="keyDownNumber()" >
           </asp:TextBox>
       </itemtemplate>
 </asp:templatefield>
</Columns>



For DataGridView you can refer following links:

datagridview-data-entry-validation/[^]
Data Entry Validation in DataGridView[^]
datagridview-cell-validation[^]
 
Share this answer
 
v4

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