Gridview dynamic binding based on dropdown list selection

(ASP.NET)


Hi,
In this article i am binding the gridview dynamically based on the dropdown list selection.

I binded the dropdownlist with employees birthdates. On the selection of employee birthdate from dropdownlist, the gridview will show the employees record for that specific birth date.


.aspx page

 <div>
        <asp:DropDownList ID="drp_birthdate" OnSelectedIndexChanged="drp_birthdate_SelectedIndexChanged" AutoPostBack="true"  DataTextField="BirthDate"   dataTextFormatString="{0:yyyy-MM-dd}" DataValueField="EmployeeID" runat="server">
        </asp:DropDownList>
        <br />
        <br />
          <asp:GridView  AutoGenerateColumns="False"  ID="grdvw_employees" runat="server">
           <Columns>
               <asp:BoundField DataField="BirthDate" DataFormatString="{0:yyyyMM}" />
               <asp:BoundField DataField="EmployeeID"  HeaderText="Employee ID" />
               <asp:BoundField DataField="FirstName"  HeaderText="FirstName" />
               <asp:BoundField DataField="LastName"  HeaderText="LastName" />
               <asp:BoundField DataField="Title"  HeaderText="Title" />
           </Columns>
        </asp:GridView>
    </div>


Codebehind file:-

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                binddropdownList();

                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

                string query = "Select* from Employees";

                DataSet ds = new DataSet();

                SqlDataAdapter sqladp = new SqlDataAdapter(query, sqlcon);

                sqladp.Fill(ds);

                grdvw_employees.DataSource = ds;
                grdvw_employees.DataBind();
            }
        }

        private void binddropdownList()
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

                string query = "Select EmployeeID,BirthDate from Employees";

                DataSet ds = new DataSet();

                SqlDataAdapter sqladp = new SqlDataAdapter(query, sqlcon);

                sqladp.Fill(ds);

                drp_birthdate.DataSource = ds;
                drp_birthdate.DataBind();


            }
            catch (Exception)
            { }
        }

        private void bindgridView(DateTime date)
{
    try
    {

   
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

        sqlcon.Open();

        string query = "select * from Employees where BirthDate = @birthDate";
        SqlCommand cmd = new SqlCommand(query, sqlcon);
        SqlParameter birthdate = cmd.Parameters.Add("@birthDate", SqlDbType.DateTime);
        birthdate.Value = date;
        SqlDataReader rdr = cmd.ExecuteReader();

        grdvw_employees.DataSource = rdr;
        grdvw_employees.DataBind();

        sqlcon.Close();
    }
    catch (Exception)
    { }
}

        protected void drp_birthdate_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string birthDate = drp_birthdate.SelectedItem.Text;
             
                bindgridView(Convert.ToDateTime(birthDate));
            }
            catch (Exception)
            {

            }
        }

Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/10/2013
6/10/2013
Download PDF

Comments