Monday, February 20, 2012

Bind Data Dynamically To Drop Down List in gridview in

 ASPX Code:

 <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#EDEDED" HeaderStyle-ForeColor="#184F46" HeaderStyle-Height="40px" HeaderStyle-HorizontalAlign="Center" OnRowDataBound="Gridview1_RowDataBound" SelectedRowStyle-BackColor="#F9CACA" ShowFooter="True" TabIndex="9" Width="100%">
<asp:TemplateField HeaderStyle-Height="30px" HeaderText="Package Type">
 <asp:DropDownList ID="ddlPackageType" runat="server"  Width="70px">
  <HeaderStyle Height="30px" Width="8%" />
  <ItemStyle HorizontalAlign="Center" />

CODE Behind Code for Binding Data  :

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
            DataSet dsPackageType = new DataSet();
      //Write Code For Getting data from Database and add it in dsPackageType Dataset .        

           if (e.Row.RowType == DataControlRowType.DataRow)
                Control ctrl = e.Row.FindControl("ddlPackageType");
                if (ctrl != null)

                    DropDownList dd = ctrl as DropDownList;
                    dd.DataTextField = "PackageType";
                    dd.DataValueField= "Id";
                    dd.DataSource = dsPackageType;
                    dd.Items.Insert(0, new ListItem("- Select -", "0"));



No comments:

Post a Comment