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


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to validate the gridview

Posted By: Suresh     Posted Date: November 05, 2008    Points:2   Category :ASP.Net
How to validate the controls in the gridview during runtime. when im insert the value in the gridview, it should validate my mail-id. how? can u please explain throgh code behind c@.
this is my code for validate the code during update.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

//Checking EditIndex should be same as current Row Index, then bind the validation controls in that particular row.

if (GridView1.EditIndex == e.Row.RowIndex)
{
RegularExpressionValidator regexpvalidator = new RegularExpressionValidator();
regexpvalidator.ID = "RegularExpressionValidator1";
regexpvalidator.ValidationExpression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
regexpvalidator.ControlToValidate = "TextBox4";
regexpvalidator.ErrorMessage = "Invalid Email Id";
regexpvalidator.SetFocusOnError = true;
e.Row.Cells[3].Controls.Add(regexpvalidator);

}
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


TextBox TextBox1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("TextBox1"));
TextBox TextBox2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox2"));
TextBox TextBox3 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox3"));
TextBox TextBox4 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox4"));
TextBox TextBox5 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].FindControl("TextBox5"));

string Age = "0";

if (TextBox3.Text.Trim() != String.Empty)
Age = TextBox3.Text;

string sql = "UPDATE Customertest " + "SET Cus_Name='" + TextBox1.Text + "', Cus_Sex='" +
TextBox2.Text + "', Cus_Age='" + Age + "', Cus_City='" + TextBox5.Text + "', Cus_Email='" +
TextBox4.Text + "' " + "WHERE Cus_Code= " + GridView1.DataKeys[e.RowIndex].Value;

using (SqlConnection conn = new SqlConnection("Data Source=HOME-932952CF89;Initial Catalog=test;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

GridView1.EditIndex = -1;
BindGridView();

}

Responses
Author: Faizal             
Posted Date: November 05, 2008     Points: 5   

Hi,
I assume that you have template column insite gridview wheer user enter their mail id if yes then you can have regular expression validator control to validate users mail id.

Author: Suresh             
Posted Date: November 05, 2008     Points: 5   

i need that in code.

Author: Faizal             
Posted Date: November 05, 2008     Points: 5   

HI Suresh,
Just modify you GridView code like this for adding textBox with Validation control in a GridView


<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Invalid Email"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">Invalid Email</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Author: Suresh             
Posted Date: November 05, 2008     Points: 5   

Thank you faizal. but i want to validate during runtime. for example below code represent the validation for mail-id during run time. like wise i need for insert .


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

//Checking EditIndex should be same as current Row Index, then bind the validation controls in that particular row.

if (GridView1.EditIndex == e.Row.RowIndex)
{
RegularExpressionValidator regexpvalidator = new RegularExpressionValidator();
regexpvalidator.ID = "RegularExpressionValidator1";
regexpvalidator.ValidationExpression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
regexpvalidator.ControlToValidate = "TextBox4";
regexpvalidator.ErrorMessage = "Invalid Email Id";
regexpvalidator.SetFocusOnError = true;
e.Row.Cells[3].Controls.Add(regexpvalidator);

}
}
}

Author: Suresh             
Posted Date: November 05, 2008     Points: 5   

code i need through code behind C sharp in asp.net

Author: Faizal             
Posted Date: November 05, 2008     Points: 5   

Hi Suresh,
Your code seems correct to me just remove this line and check

if (GridView1.EditIndex == e.Row.RowIndex)
{

}

and keep all this

RegularExpressionValidator regexpvalidator = new RegularExpressionValidator();
regexpvalidator.ID = "RegularExpressionValidator1";
regexpvalidator.ValidationExpression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
regexpvalidator.ControlToValidate = "TextBox4";
regexpvalidator.ErrorMessage = "Invalid Email Id";
regexpvalidator.SetFocusOnError = true;
e.Row.Cells[3].Controls.Add(regexpvalidator);


Author: Suresh             
Posted Date: November 06, 2008     Points: 5   

Hi faizal,Above code is working properly, above code vil raise when we make any update. i want code for insertion.should be in code behind C sharp.
this is my entire code.
----------------------------------------------------------

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}

private void BindGridView()
{
string cons = "Data Source=HOME-932952CF89;Initial Catalog=test;Integrated Security=True";
string sql = "Select * from CustomerTest Order By Cus_Name";
SqlConnection con = new SqlConnection(cons);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
con.Open();
DataTable dt = new DataTable();
da.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

//Checking EditIndex should be same as current Row Index, then bind the validation controls in that particular row.

if (GridView1.EditIndex == e.Row.RowIndex)
{

CustomValidator customValidator = new CustomValidator();
customValidator.ID = "CustomValidator1";
customValidator.ClientValidationFunction = "fnClientFunction";
customValidator.ControlToValidate = "TextBox2";
customValidator.ErrorMessage = "Invalid Gender";
customValidator.SetFocusOnError = true;
customValidator.ValidateEmptyText = true;
e.Row.Cells[1].Controls.Add(customValidator);

RangeValidator rangevalidator = new RangeValidator();
rangevalidator.ID = "RangeValidator1";
rangevalidator.ControlToValidate = "TextBox3";
rangevalidator.ErrorMessage = "Invalid Age";
rangevalidator.MaximumValue = "100";
rangevalidator.MinimumValue = "0";
rangevalidator.SetFocusOnError = true;
rangevalidator.Type = ValidationDataType.Integer;
e.Row.Cells[2].Controls.Add(rangevalidator);

RegularExpressionValidator regexpvalidator = new RegularExpressionValidator();
regexpvalidator.ID = "RegularExpressionValidator1";
regexpvalidator.ValidationExpression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
regexpvalidator.ControlToValidate = "TextBox4";
regexpvalidator.ErrorMessage = "Invalid Email Id";
regexpvalidator.SetFocusOnError = true;
e.Row.Cells[3].Controls.Add(regexpvalidator);

RequiredFieldValidator reqfldVal = new RequiredFieldValidator();
reqfldVal.ID = "RequiredValidator10";
reqfldVal.ControlToValidate = "TextBox5";
reqfldVal.ErrorMessage = "City Required";
reqfldVal.SetFocusOnError = true;
e.Row.Cells[4].Controls.Add(reqfldVal);


}
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


TextBox TextBox1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("TextBox1"));
TextBox TextBox2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox2"));
TextBox TextBox3 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox3"));
TextBox TextBox4 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox4"));
TextBox TextBox5 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].FindControl("TextBox5"));

string Age = "0";

if (TextBox3.Text.Trim() != String.Empty)
Age = TextBox3.Text;

string sql = "UPDATE Customertest " + "SET Cus_Name='" + TextBox1.Text + "', Cus_Sex='" +
TextBox2.Text + "', Cus_Age='" + Age + "', Cus_City='" + TextBox5.Text + "', Cus_Email='" +
TextBox4.Text + "' " + "WHERE Cus_Code= " + GridView1.DataKeys[e.RowIndex].Value;

using (SqlConnection conn = new SqlConnection("Data Source=HOME-932952CF89;Initial Catalog=test;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

GridView1.EditIndex = -1;
BindGridView();

}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtName = (TextBox)GridView1.FooterRow.FindControl("txtName");
TextBox txtGender = (TextBox)GridView1.FooterRow.FindControl("txtGender");
TextBox txtAge = (TextBox)GridView1.FooterRow.FindControl("txtAge");
TextBox txtMail = (TextBox)GridView1.FooterRow.FindControl("txtMail");
TextBox txtCity = (TextBox)GridView1.FooterRow.FindControl("txtCity");






string sql = "insert into Customertest values('" + txtName.Text + "','" + txtGender.Text + "','" + txtAge.Text + "','" + txtCity.Text + "','" + txtMail.Text + "')";
using (SqlConnection conn = new SqlConnection("Data Source=HOME-932952CF89;Initial Catalog=test;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

//GridView1.EditIndex = -1;
BindGridView();


}
}


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sql = "Delete from customertest where Cus_Code=" + GridView1.DataKeys[e.RowIndex].Value;
using (SqlConnection conn = new SqlConnection("Data Source=HOME-932952CF89;Initial Catalog=test;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}


BindGridView();
}


}


----------------------------------------------------------

Author: Savi3             
Posted Date: November 07, 2008     Points: 5   

Hi to all...

How can we do this in the DataGrid Control



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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