Showing posts with label Server Controls inside GridView set value using asp.net C#. Show all posts
Thursday, January 6, 2011
Server Controls inside GridView set value using asp.net C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
DataKeyNames="EmpID" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="SetEno">
<ItemTemplate>
<asp:DropDownList ID="ddlEmpid" runat="server" Width="90%" DataSourceID="SqlDataSource1" DataTextField="EMPID"
DataValueField="EMPID" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Empid" Visible="false">
<ItemTemplate>
<asp:Label ID="lblEmpNo" runat="server" Text='<%# Eval("EmpNo") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EMPID" HeaderText="EmployeeID" ReadOnly="true" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile" />
</Columns>
</asp:GridView>
Step2:-
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mycon %>"
SelectCommand="SELECT [EMPID] FROM EMPLOYEEID"></asp:SqlDataSource>
step3:-
public partial class GridViewDropDown : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
string conection;
conection = System.Configuration.ConfigurationManager.ConnectionStrings["mycon"].ConnectionString.ToString();
con = new SqlConnection(conection);
FillGrid();
}
protected void FillGrid()
{
SqlCommand cmd = new SqlCommand("select * from employee", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlEmpid");
Label lblEmpid = ((Label)e.Row.FindControl("lblEmpNo"));
// string y = (String)DataBinder.Eval(e.Row.DataItem, "FirstName");
string x=lblEmpid.Text.ToString().Trim();
ddl.SelectedValue = x;
}
}
}
No comments:
Post a Comment