Monday, December 29, 2008

Validate DropDownList inside GRIDVIEW

0 comments 12/29/2008
Sign up to receive our site updates!
In Coding Page









<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound" Font-Names="Verdana" Font-Size="Small" style="z-index: 100; left: 41px; position: absolute; top: 66px" DataKeyNames="productid">



<Columns>



<asp:BoundField DataField="productid" HeaderText="ProductID" />



<asp:BoundField DataField="productname" HeaderText="ProductName" />



<asp:BoundField DataField="price" HeaderText="Price" />



<asp:TemplateField HeaderText="Choose">



<ItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server" Width="100px">asp:DropDownList><br />

ItemTemplate>

<EditItemTemplate>



<asp:DropDownList ID="DropDownList1" runat="server" Width="100px">asp:DropDownList><br />



<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropDownList1" Display="dynamic"

ErrorMessage="Please Select the Rating" InitialValue="Select Rating">asp:RequiredFieldValidator>



EditItemTemplate>

asp:TemplateField>



<asp:CommandField ShowEditButton="True" />



Columns>



asp:GridView>





In .CS File



using System.Data.SqlClient;



public partial class _Default : System.Web.UI.Page

{

SqlConnection con; SqlDataAdapter da; SqlCommand cmd; DataSet ds;

protected void Page_Load(object sender, EventArgs e)

{

con = new SqlConnection("server=FOCALPOI-392A7D\\SQLEXPRESS;user id=raji;password=chekuri;database=raji");

if (IsPostBack == false)

{

getdata();

}

}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{



if (e.Row.RowType == DataControlRowType.DataRow)

{



DropDownList ddList = (DropDownList)e.Row.FindControl("DropDownList1");

//DropDown List First item is same as InitialValue propertie

ddList.Items.Add("Select Rating");

ddList.Items.Add("1");

ddList.Items.Add("2");

ddList.Items.Add("3");

ddList.Items.Add("4");

ddList.Items.Add("5");





}



}



public void getdata()

{

da = new SqlDataAdapter("select * from product", con);

ds = new DataSet();

da.Fill(ds, "product");

GridView1.DataSource = ds;

GridView1.DataBind();

}





protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

getdata();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

getdata();

}



//we are not doing any updation

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

GridView1.EditIndex = -1;

getdata();

}

}





Your Ad Here

0 comments:

Respects for your's Questions & Opinions

  • Friends
  •  

    Recent Posts

    Copyright 2008 All Rights Reserved Tech Tasks Template by Rajesh Kumar Chekuri