从一个表中选择下拉列表值并显示另一个表ASP.NET中的gridview数据

时间:2015-03-25 05:24:40

标签: c# asp.net gridview drop-down-menu

我创建了一个Web应用程序,我有一个下拉列表和一个Gridview。我从DB填充下拉值,我需要在网格视图中显示所选值的数据,但需要在另一个表中显示数据。我不知道如何做到这一点任何人都可以帮助我。我的代码如下。提前谢谢。

背后的代码

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                dropDownData.DataSource = dt;
                dropDownData.DataTextField = "Work_Packages";
                dropDownData.DataValueField = "Work_Packages";
                dropDownData.DataBind();
                Bind_Gridview();
             }
        }
        catch (Exception ex)
        {
            string msg = ex.Message.ToString();
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('" + ex.Message + "');</script>", false);
        }
    }


    public void Displaysearchresult(string strSearch)
    {
        SqlCommand cmd = new SqlCommand("Select * from ArR_Station_Work_Packages Where Station_Name like'" + txtstationName.Text + "%'", myConnection);
        cmd.Parameters.AddWithValue("@Work_Packages", strSearch);
        cmd.Connection.Open();
        dropDownData.DataSource = cmd.ExecuteReader();
        dropDownData.DataTextField = "Work_Packages";
        dropDownData.DataValueField = "Work_Packages";
        dropDownData.DataBind();
    }
    protected void GetWorkPackages(object sender, EventArgs e)
    {
        if (txtstationName.Text == "" | txtstationName.Text == null)
        {

        }
        else
        {
            Displaysearchresult(txtstationName.Text);
        }
    }
    protected void ddlSelectedData(object sender, EventArgs e)
    {
        Bind_Gridview();
    }
    private void Bind_Gridview()
    {
        SqlCommand cmd = new SqlCommand("Select * from ArR_WP_BOQ_Details Where BOQ_ID like'" + dropDownData.SelectedValue + "'", myConnection);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        dt.Clear();
        da.Fill(dt);
        myConnection.Close();
        ViewState["BOQ"] = null;
        ViewState["BOQ"] = (DataTable)dt;
        addnewrow();
        grd_BOQ.DataSource = dt;
        grd_BOQ.DataBind();
    }

    private void addnewrow()
    {
        if (ViewState["BOQ"] != null)
        {
            DataTable idt = (DataTable)ViewState["BOQ"];
            DataRow drCurrentRow = null;
            DataRow drCurrentRow_lastrow = null;
            int New_gridrowcount = idt.Rows.Count;
            drCurrentRow = idt.NewRow();
            drCurrentRow_lastrow = idt.NewRow();

            if (idt.Rows.Count >= 10)
            {
                idt.Rows.InsertAt(drCurrentRow, 9);
                idt.Rows.InsertAt(drCurrentRow_lastrow, idt.Rows.Count);
            }
            else
            {
                idt.Rows.Add(drCurrentRow);
            }
            ViewState["BOQ"] = idt;
        }
    }
    protected void grd_BOQ_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        Bind_Gridview();
        grd_BOQ.PageIndex = e.NewPageIndex;
        grd_BOQ.EditIndex = -1;
        grd_BOQ.DataBind();
    }

    protected void grd_BOQ_RowCreated(object sender, GridViewRowEventArgs e)
    {
        try
        {
            int grd_lastpgcount = grd_BOQ.PageIndex;
            dt = (DataTable)ViewState["BOQ"];
            int count = dt.Rows.Count;
            if (e.Row.RowType == DataControlRowType.DataRow || !string.IsNullOrEmpty(dropDownData.SelectedValue))
            {
                int rowcreate_rowindex = e.Row.RowIndex;
                if (rowcreate_rowindex < count - 1 && rowcreate_rowindex != 9 && grd_lastpgcount * 10 + rowcreate_rowindex + 1 < count)
                {
                    for (int rc = 0; rc < 1; rc++)
                    {
                        edit_button = new ImageButton();
                        edit_button.ID = "edit_button";
                        edit_button.ImageUrl = "~/images/edit.png";
                        edit_button.Style.Add("width", "20px");
                        edit_button.Style.Add("height", "20px");
                        edit_button.CommandName = "Edit";
                        edit_button.ToolTip = "Edit";
                        e.Row.Cells[rc].Controls.Add(edit_button);

                        update_button = new ImageButton();
                        update_button.ID = "update_button";
                        update_button.ImageUrl = "~/images/save.png";
                        update_button.Style.Add("width", "20px");
                        update_button.Style.Add("height", "20px");
                        update_button.CommandName = "Update";
                        update_button.ToolTip = "Update";
                        e.Row.Cells[rc].Controls.Add(update_button);
                        if (onrow_edit_flag == rowcreate_rowindex)
                        {
                            edit_button.Visible = false;
                            update_button.Visible = true;
                        }
                        else
                        {
                            edit_button.Visible = true;
                            update_button.Visible = false;
                        }
                    }
                }
                else if (rowcreate_rowindex == count - 1 || rowcreate_rowindex == 9 || grd_lastpgcount * 10 + rowcreate_rowindex + 1 == count)
                {
                    for (int rc = 0; rc < 1; rc++)
                    {
                        add_button = new ImageButton();
                        add_button.ID = "add_button";
                        add_button.ImageUrl = "~/images/Add.png";
                        add_button.Style.Add("width", "20px");
                        add_button.Style.Add("height", "20px");
                        add_button.Click += new ImageClickEventHandler(insert_button_Click);
                        add_button.ToolTip = "Add";
                        e.Row.Cells[rc].Controls.Add(add_button);

                        update_button = new ImageButton();
                        update_button.ID = "update_button";
                        update_button.ImageUrl = "~/images/save.png";
                        update_button.Style.Add("width", "20px");
                        update_button.Style.Add("height", "20px");
                        update_button.Click += new ImageClickEventHandler(insert_save_click);
                        update_button.ToolTip = "Add";
                        e.Row.Cells[rc].Controls.Add(update_button);

                        if (onrow_create_flag == rowcreate_rowindex || dt.Rows.Count % grd_BOQ.PageSize == onrow_create_flag)
                        {
                            add_button.Visible = false;
                            update_button.Visible = true;
                        }
                        else
                        {
                            add_button.Visible = true;
                            update_button.Visible = false;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string msg = ex.Message.ToString();
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('" + msg + "');</script>", false);
        }
    }

0 个答案:

没有答案