locked
How to add datatextfield and datavaluefield from dropdownlist to gridview? RRS feed

  • Question

  • User-367318540 posted

    i want to insert data datatextfield and datavaluefield from dropdownlist to gridview....

    SqlConnection con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SPS;MultipleActiveResultSets=True;");
    
            DataTable dt = new DataTable();
            DataRow dr;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    dt.Columns.Add("Codeitem");
                   // dt.Columns.Add("Descriptionitem");
                    dt.Columns.Add("QTY");
                    ViewState["dt"] = dt;
                    itemload();
                }
            }
    
            private void itemload()
            {
                con.Open();
                SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
                DataSet dspr1 = new DataSet();
                adpr1.Fill(dspr1);
                DropDownList1.DataSource = dspr1.Tables[0];
                DropDownList1.DataTextField = "Descriptionitem";
                DropDownList1.DataValueField = "Codeitem";
                DropDownList1.DataBind();
            }
    
            protected void GVadd_Click(object sender, EventArgs e)
            {
                dt = ViewState["dt"] as DataTable;
                dr = dt.NewRow();
                dr["Codeitem"] = DropDownList1.SelectedValue;
               // dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();
    
                dr["QTY"] = txtqty.Text;
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                clear();
            }
    
            private void clear()
            {
               // Codeitem.Text = "";
                txtqty.Text = "";
            }
        }
    }

    Please guide....

    Wednesday, August 7, 2019 4:53 AM

Answers

  • User-719153870 posted

    Hi akhterr,

    It seems you have posted twice for this subject, and have marked an answer:https://forums.asp.net/t/2158515.aspx.

    I have tested your current behind code along with the front code you provided earlier in above thread. After i removed the comments in your code, everything worked well.

    Is there a new problem you are facing with? Please tell.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 7, 2019 5:34 AM