Click here to Skip to main content
16,017,852 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

how can I make Gridview cell from accepting only one decimal point like I did for Textbox?

C#
private void txtDiscount_KeyPress(object sender, KeyPressEventArgs e)
{
  if (e.KeyChar == '.')
  {
    if (txtDiscount.Text.Contains('.'))
      e.Handled = true;
  }
}


But for the gridview cell it's not working. Can you help me?
Posted
Updated 19-Dec-10 23:54pm
v2
Comments
JF2015 20-Dec-10 5:54am    
Edited to fix minor spelling issues and improve code formatting.

For Child Controls in the Gridview we don't have the direct events

so first we have to get the TextBox value like this

TextBox Foottxtboxunitrate = (TextBox)ProjectOnFormGrid.FooterRow.FindControl("Foottxtboxunitrate");


then

C#
Regex amountpattern = new Regex(@"^[0-9]*(\.)?[0-9]{0,1}?$");
 if (amountpattern.IsMatch(Foottxtboxunitrate.Text.Trim()) == false)
        {
 lblError.Text = "Enter valid Rate amount with maximum 1 decimal places. ";
 lblError.Visible = true;
 Foottxtboxunitrate.Focus();
 return;
        }
 
Share this answer
 
v2
You can achieve that by using javascript

GridView Calculation with JavaScript - Formatting and Validation[^]
 
Share this answer
 

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