locked
Bind (Set) DropDownList Selected Value in GridView EditItemTemplate in ASP.Net RRS feed

  • Question

  • User1151703306 posted

    Hello everyone, I need your help.

    I need populate dropdownlist selected value getting from database in gridview edititemtemplate in asp.net using c#.

    I can't do it because when edit the gridview edititemtemplate in the dropdown lists the values of default are selected and not those registered in the database.

    How to do resolve this ?

    Can you help me ?

    My code below

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView g2 = (GridView)sender;
            g2.EditIndex = e.NewEditIndex;
    
            GridViewRow gvCustomerRow = g2.NamingContainer as GridViewRow;
            int customerId = (int)gvProducts.DataKeys[gvCustomerRow.RowIndex].Value;
    
            sql = @String.Format(" SELECT * FROM `doTable` ");
            sql += String.Format(" WHERE sID IN ('{0}') ", customerId);
    
            g2.DataSource = GetData(sql);
            g2.DataBind();
    
            DateTime dateofBirth = new DateTime();
            ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
            {
                MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                MonthNumber = a
            });
            ddlMonth.DataBind();
    
            DropDownList ddlYear = (DropDownList)(g2.Rows[g2.EditIndex].FindControl("ddlYear"));
            ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();
            ddlYear.DataBind();
            ddlYear.Items.Insert(0, new ListItem("0001", "0001"));
    
            ddlday.SelectedValue = dateofBirth.Day.ToString();
            ddlMonth.SelectedValue = dateofBirth.Month.ToString();
            ddlYear.SelectedValue = dateofBirth.Year.ToString();
    
            ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
            ddlday.DataBind();
        }

    Saturday, October 5, 2019 7:05 PM

Answers

  • User314352500 posted

    If I understand your problem :

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView g2 = (GridView)sender;
            g2.EditIndex = e.NewEditIndex;
    
            GridViewRow gvCustomerRow = g2.NamingContainer as GridViewRow;
            int customerId = (int)gvProducts.DataKeys[gvCustomerRow.RowIndex].Value;
    
            DataTable dts = new DataTable();
            DateTime dateofBirth = new DateTime();
    
            sql = @String.Format(" SELECT * FROM `doTable` ");
            sql += String.Format(" WHERE sID IN ('{0}') ", customerId);
    
            using (OdbcConnection cn =
              new OdbcConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString))
            {
                using (OdbcCommand cmd =
                    new OdbcCommand(sql, cn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection.Open();
    
                    using (OdbcDataAdapter adapt =
                        new OdbcDataAdapter(cmd))
                    {
                        adapt.Fill(dts);
                        if (dts.Rows.Count > 0)
                        {
                            g2.DataSource = dts;
                            g2.DataBind();
    
                            DataRow row = dts.Rows[0];
    
                            if (String.IsNullOrEmpty(row["DateValuefromDB"].ToString()))
                            {
                                ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
                                {
                                    MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                                    MonthNumber = a
                                });
                                ddlMonth.DataBind();
                                DropDownList ddlYear = (DropDownList)(g2.Rows[g2.EditIndex].FindControl("ddlYear"));
                                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();
                                ddlYear.DataBind();
                                ddlYear.Items.Insert(0, new ListItem("0001", "0001"));
                                ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
                                ddlday.DataBind();
    
                                ddlday.SelectedValue = dateofBirth.Day.ToString();
                                ddlMonth.SelectedValue = dateofBirth.Month.ToString();
                                ddlYear.SelectedValue = dateofBirth.Year.ToString();
                            }
                            else
                            {
                                ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
                                {
                                    MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                                    MonthNumber = a
                                });
                                ddlMonth.DataBind();
                                DropDownList ddlYear = (DropDownList)(g2.Rows[g2.EditIndex].FindControl("ddlYear"));
                                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();
                                ddlYear.DataBind();
                                ddlYear.Items.Insert(0, new ListItem("0001", "0001"));
                                ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
                                ddlday.DataBind();
    
                                ddlday.SelectedValue = Convert.ToDateTime(row["DateValuefromDB"]).Day.ToString();
                                ddlMonth.SelectedValue = Convert.ToDateTime(row["DateValuefromDB"]).Month.ToString();
                                ddlYear.SelectedValue = Convert.ToDateTime(row["DateValuefromDB"]).Year.ToString();
                            }
                        }
                    }
                }
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 6, 2019 6:27 AM

All replies

  • User475983607 posted

    You've limited the dropdown list to today plus 12 years in the future. 

    ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();

    Clearly, date of birth cannot be in the future.  

    Saturday, October 5, 2019 7:13 PM
  • User314352500 posted

    If I understand your problem :

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView g2 = (GridView)sender;
            g2.EditIndex = e.NewEditIndex;
    
            GridViewRow gvCustomerRow = g2.NamingContainer as GridViewRow;
            int customerId = (int)gvProducts.DataKeys[gvCustomerRow.RowIndex].Value;
    
            DataTable dts = new DataTable();
            DateTime dateofBirth = new DateTime();
    
            sql = @String.Format(" SELECT * FROM `doTable` ");
            sql += String.Format(" WHERE sID IN ('{0}') ", customerId);
    
            using (OdbcConnection cn =
              new OdbcConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString))
            {
                using (OdbcCommand cmd =
                    new OdbcCommand(sql, cn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection.Open();
    
                    using (OdbcDataAdapter adapt =
                        new OdbcDataAdapter(cmd))
                    {
                        adapt.Fill(dts);
                        if (dts.Rows.Count > 0)
                        {
                            g2.DataSource = dts;
                            g2.DataBind();
    
                            DataRow row = dts.Rows[0];
    
                            if (String.IsNullOrEmpty(row["DateValuefromDB"].ToString()))
                            {
                                ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
                                {
                                    MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                                    MonthNumber = a
                                });
                                ddlMonth.DataBind();
                                DropDownList ddlYear = (DropDownList)(g2.Rows[g2.EditIndex].FindControl("ddlYear"));
                                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();
                                ddlYear.DataBind();
                                ddlYear.Items.Insert(0, new ListItem("0001", "0001"));
                                ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
                                ddlday.DataBind();
    
                                ddlday.SelectedValue = dateofBirth.Day.ToString();
                                ddlMonth.SelectedValue = dateofBirth.Month.ToString();
                                ddlYear.SelectedValue = dateofBirth.Year.ToString();
                            }
                            else
                            {
                                ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
                                {
                                    MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                                    MonthNumber = a
                                });
                                ddlMonth.DataBind();
                                DropDownList ddlYear = (DropDownList)(g2.Rows[g2.EditIndex].FindControl("ddlYear"));
                                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();
                                ddlYear.DataBind();
                                ddlYear.Items.Insert(0, new ListItem("0001", "0001"));
                                ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
                                ddlday.DataBind();
    
                                ddlday.SelectedValue = Convert.ToDateTime(row["DateValuefromDB"]).Day.ToString();
                                ddlMonth.SelectedValue = Convert.ToDateTime(row["DateValuefromDB"]).Month.ToString();
                                ddlYear.SelectedValue = Convert.ToDateTime(row["DateValuefromDB"]).Year.ToString();
                            }
                        }
                    }
                }
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 6, 2019 6:27 AM
  • User1151703306 posted

    Thank you!

    Sunday, October 6, 2019 1:58 PM