changeindex droupdownlist dynamic bind gridview

48 Views Asked by At

The description is clear in the image by choosing Combo_Categoreis create gridview and change value DropDown_Value postback get data

Thanks

protected void Combo_Categoreis_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection Connection = new SqlConnection(connectionString: ConfigurationSettings.AppSettings["ConStr"]);
    Connection.Open();
    SqlCommand Command = new SqlCommand("select * from dbo.Tbl_Defualt_Product_Features  where  Categoreis_Id = "+ Combo_Categoreis.SelectedItem.Value, Connection);
    Command.CommandType = CommandType.Text;
    SqlDataReader reader = Command.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Id", typeof(Int64)));
    dt.Columns.Add(new DataColumn("fproperties", typeof(string)));

    while (reader.Read())
    {
        dt.Rows.Add(reader["Id"],reader["Display_name"].ToString());
    }

    ViewState["CurrentTable"] = dt;
    Gridview1.DataKeyNames = new string[] { "Id" };
    Gridview1.DataSource = dt;
    Gridview1.DataBind();


protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    SqlConnection Connection = new SqlConnection(connectionString: ConfigurationSettings.AppSettings["ConStr"]);
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Connection.Open();
        var ddl = (DropDownList)e.Row.FindControl("DropDown_Value");
        SqlCommand cmd = new SqlCommand("SELECT * FROM Tbl_Defualt_Value_Feathures WHERE Defualt_Product_Features_Id= " + e.Row.Cells[1].Text.ToString(), Connection);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Connection.Close();
        ddl.DataSource = ds;
        ddl.DataTextField = "Value";
        ddl.DataValueField = "Id";
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("انتخاب مقدار", "0"));
    }
}
0

There are 0 best solutions below