Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to validate the cells inside the grid view bcoz i hav updated the cells but i dono how to validate the cells inside grid view. Kindly suggest me with some options.
Posted

Add all your required validators to your mark up and enable/disable them in RowDataBound
this example use compare validator that validate a test value:
C#
void Grd_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
      DataRowView rowView = (DataRowView)e.Row.DataItem;
      String testValue = rowView["Test"];
      CompareValidator cv1 = (CompareValidator)e.Row.FindControl("cv1); // checks between 2-7
      CompareValidator cv2 = (CompareValidator)e.Row.FindControl("cv2); // checks true/false
      cv1.Enabled = testValue.ToUpper().Equals("ABC");
      cv2.Enabled = !cv1.Enabled;
  }
}
 
Share this answer
 
you can do it by applying js on that gridview control on row data bound event
here is simple example

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
TextBox tbx0 = (TextBox)e.Row.Cells[0].Controls[1];

tbx0.Attributes.Add("onblur", "return JSFunction(" + tbx0.ClientID + ");"

}
}
 
Share this answer
 
v2
hi if it is dynamic gridview try this in edititem template.

case ListItemType.EditItem:

TextBox field_txtbox = new TextBox();
field_txtbox.ID = FieldName;
field_txtbox.Text = String.Empty;
field_txtbox.Width =60;
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ID = field_txtbox.ID + "valid";
rfv.ControlToValidate = field_txtbox.ID;
rfv.ErrorMessage = "Please Enter Value";
rfv.Display = ValidatorDisplay.Dynamic;
Container.Controls.Add(rfv);
 
Share this answer
 
You can add validators in item template and attach it to the control u want , validation will automatically take place on each row , if u want to do dserver side validation then custom validator,,, hope this works as a shortcut and better solution
 
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