如何将DropDownList选中的值插入到asp.net的GridView中?

时间:2014-05-05 11:56:00

标签: c# asp.net

    using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
     using System.Web.UI;
     using System.Web.UI.WebControls;
     using System.Data;
     using System.Drawing;

    namespace NewDPOcopy
    {
      public partial class UpdatePaperCost : System.Web.UI.Page
     {
       Services.ServiceAuthentication ser = new Services.ServiceAuthentication();
       // Services.ServiceAuthentication ser = new Services.ServiceAuthentication();

        protected void Page_Load(object sender, EventArgs e)
        {
            fillgrodPaperType();
            ddlcatid.Visible = true;
            ddlcatid.DataSource = ser.Getpapercategory();
            ddlcatid.DataTextField = "Cat_Name";
            //ddlcatid.DataValueField = "Cat_id";
            ddlcatid.DataBind();
        }

        protected void fillgrodPaperType()
        {
            Services.PaperTypes[] PaperInfo = ser.GetALLPaperTypeInfo();

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("pt_id", typeof(string)));
            dt.Columns.Add(new DataColumn("pt_name", typeof(string)));
            dt.Columns.Add(new DataColumn("pt_weight", typeof(int)));

            dt.Columns.Add(new DataColumn("pt_size", typeof(string)));
            dt.Columns.Add(new DataColumn("pt_cost_per_sheet", typeof(double)));
            dt.Columns.Add(new DataColumn("pt_status", typeof(string)));


            dt.Columns.Add(new DataColumn("pt_used_by_247", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("pt_front_finish", typeof(string)));
            dt.Columns.Add(new DataColumn("pt_back_finish", typeof(string)));

            dt.Columns.Add(new DataColumn("pt_default", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("pt_colour", typeof(string)));
            dt.Columns.Add(new DataColumn("pt_nexpress", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("pt_igen", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("Cat_Name", typeof(string)));

            for (int i = 0; i < PaperInfo.Length; i++)
            {
                DataRow dr = null;
                dr = dt.NewRow();
                dr["pt_id"] = PaperInfo[i].Pt_id;
                dr["pt_name"] = PaperInfo[i].Pt_name;
                dr["pt_weight"] = PaperInfo[i].Pt_weight;

                dr["pt_size"] = PaperInfo[i].Pt_size;
                dr["pt_cost_per_sheet"] = PaperInfo[i].Pt_cost_per_sheet;
                // dr["pt_cost_per_sheet"] = 12;
                dr["pt_status"] = PaperInfo[i].Pt_status;

                dr["pt_used_by_247"] = PaperInfo[i].Pt_used_by_247;
                dr["pt_front_finish"] = PaperInfo[i].Pt_front_finish;
                dr["pt_back_finish"] = PaperInfo[i].Pt_back_finish;

                dr["pt_default"] = PaperInfo[i].Pt_default;
                dr["pt_colour"] = PaperInfo[i].Pt_colour;
                dr["pt_nexpress"] = PaperInfo[i].Pt_nexpress;
                dr["pt_igen"] = PaperInfo[i].Pt_igen;
                dr["Cat_Name"] = PaperInfo[i].Cat_Id;
                dt.Rows.Add(dr);
            }

            GrdvwPTID.DataSource = dt;
            GrdvwPTID.DataBind();

            //ddlPTID.DataSource = dt;
            //ddlPTID.DataTextField = "pt_name";
            //ddlPTID.DataValueField = "pt_id";
            //ddlPTID.DataBind();
        }


        protected void ClearPaperType()
        {
            txtptid.Text = "";
            txtptname.Text = "";
            txtptweight.Text = "";
            txtptsize.Text = "";
            txtptcostpersheet.Text = "";
            txtptstatus.Text = "";
            txtptfontfinish.Text = "";
            txtptbackfinish.Text = "";
            txtptcolor.Text = "";
            ddlptdefault.ClearSelection();
            ddlptdefault.Items.FindByText("<--select-->").Selected = true;
            ddlptnexpress.ClearSelection();
            ddlptnexpress.Items.FindByText("<--select-->").Selected = true;
            ddlptusedby247.ClearSelection();
            ddlptusedby247.Items.FindByText("<--select-->").Selected = true;
            ddlptigen.ClearSelection();
            ddlptigen.Items.FindByText("<--select-->").Selected = true;
            lblMessagePTYPE.Visible = false;
            HiddenField2.Value = "";
        }

        protected void GrdvwPTID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GrdvwPTID_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            int index = Convert.ToInt32(e.CommandArgument);
            Session["INDEX"] = index;

            lblMessagePTYPE.Visible = false;

            txtptid.Text = GrdvwPTID.Rows[index].Cells[1].Text;
            txtptname.Text = GrdvwPTID.Rows[index].Cells[2].Text;
            txtptweight.Text = GrdvwPTID.Rows[index].Cells[3].Text;
            txtptsize.Text = GrdvwPTID.Rows[index].Cells[4].Text;
            txtptcostpersheet.Text = GrdvwPTID.Rows[index].Cells[5].Text;
            txtptstatus.Text = GrdvwPTID.Rows[index].Cells[6].Text;

            ddlptusedby247.ClearSelection();
            if (GrdvwPTID.Rows[index].Cells[7].Text == "True")
            {
                ddlptusedby247.Items.FindByText("Yes").Selected = true;
            }
            else
            {
                ddlptusedby247.Items.FindByText("No").Selected = true;
            }

            txtptfontfinish.Text = GrdvwPTID.Rows[index].Cells[8].Text;
            txtptbackfinish.Text = GrdvwPTID.Rows[index].Cells[9].Text;
            ddlptdefault.ClearSelection();
            if (GrdvwPTID.Rows[index].Cells[10].Text == "True")
            {
                ddlptdefault.Items.FindByText("Yes").Selected = true;
            }
            else
            {
                ddlptdefault.Items.FindByText("Yes").Selected = false;
            }
            txtptcolor.Text = GrdvwPTID.Rows[index].Cells[11].Text;

            ddlptnexpress.ClearSelection();
            if (GrdvwPTID.Rows[index].Cells[12].Text == "True")
            {
                ddlptnexpress.Items.FindByText("Yes").Selected = true;
            }
            else
            {
                ddlptnexpress.Items.FindByText("No").Selected = true;
            }
            ddlptigen.ClearSelection();
            if (GrdvwPTID.Rows[index].Cells[13].Text == "True")
            {
                ddlptigen.Items.FindByText("Yes").Selected = true;
            }
            else
            {
                ddlptigen.Items.FindByText("Yes").Selected = false;
            }
            //ddlcatid.ClearSelection();
            //if (GrdvwPTID.Rows[index].Cells[14].Text == "True")
            //{
            //    ddlcatid.Items.FindByText("Yes").Selected = true;
            //}
            //else
            //{
            //    ddlcatid.Items.FindByText("Yes").Selected = false;
            //}
            HiddenField2.Value = "EditPAPERTYPE";

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (txtptid.Text == "" || txtptname.Text == "" || txtptweight.Text == "" || txtptsize.Text == "" || txtptcostpersheet.Text == "" || txtptstatus.Text == "" || txtptfontfinish.Text == "" || txtptbackfinish.Text == "" || txtptcolor.Text == "" || ddlptdefault.SelectedItem.ToString() == "<--select-->" || ddlptnexpress.SelectedItem.ToString() == "<--select-->" || ddlptusedby247.SelectedItem.ToString() == "<--select-->" || ddlptigen.SelectedItem.ToString() == "<--select-->")
            {
                lblMessagePTYPE.Visible = true;
                lblMessagePTYPE.ForeColor = Color.Red;
                lblMessagePTYPE.Text = "FILL ALL THE FIELDS AND CLICK HERE TO SUBMIT";

            }
            else
            {

                Boolean ptused247;
                if (ddlptusedby247.SelectedItem.ToString() == "Yes")
                {
                    ptused247 = true;
                }
                else
                {

                    ptused247 = false;
                }
                Boolean ptDefault;
                if (ddlptdefault.SelectedItem.ToString() == "Yes")
                {
                    ptDefault = true;
                }
                else
                {
                    ptDefault = false;
                }
                Boolean ptNexPRESS;
                if (ddlptnexpress.SelectedItem.ToString() == "Yes")
                {
                    ptNexPRESS = true;
                }
                else
                {
                    ptNexPRESS = false;
                }
                Boolean ptIgen;
                if (ddlptigen.SelectedItem.ToString() == "Yes")
                {
                    ptIgen = true;
                }
                else
                {
                    ptIgen = false;
                }


                Boolean catName;
                if (ddlcatid.SelectedItem.ToString() == "Yes")
                {
                    catName = true;
                }
                else
                {
                    catName = false;
                }
                //insert Query Call...........

                if (HiddenField2.Value != "EditPAPERTYPE")
                {
                    bool InsertResultPT = ser.InsertPaperType(txtptid.Text, txtptname.Text, Convert.ToInt32(txtptweight.Text), txtptsize.Text, Convert.ToDouble(txtptcostpersheet.Text), txtptstatus.Text, ptused247, txtptfontfinish.Text, txtptbackfinish.Text, ptDefault, txtptcolor.Text, ptNexPRESS, ptIgen, catName);
                    if (InsertResultPT)
                    {
                        lblMessagePTYPE.Visible = true;
                        lblMessagePTYPE.ForeColor = Color.Green;
                        lblMessagePTYPE.Text = "PAPER TYPE VALUES INSERTED SUCCESSFULLY!";
                        fillgrodPaperType();
                    }
                    else
                    {
                        lblMessagePTYPE.Visible = true;
                        lblMessagePTYPE.ForeColor = Color.Red;
                        lblMessagePTYPE.Text = "YOU ENTERED WRONG VALUES !";
                    }

                    // ClearPaperType();
                    txtptid.Text = "";
                    txtptname.Text = "";
                    txtptweight.Text = "";
                    txtptsize.Text = "";
                    txtptcostpersheet.Text = "";
                    txtptstatus.Text = "";
                    txtptfontfinish.Text = "";
                    txtptbackfinish.Text = "";
                    txtptcolor.Text = "";
                    ddlptdefault.ClearSelection();
                    ddlptdefault.Items.FindByText("<--select-->").Selected = true;
                    ddlptnexpress.ClearSelection();
                    ddlptnexpress.Items.FindByText("<--select-->").Selected = true;
                    ddlptusedby247.ClearSelection();
                    ddlptusedby247.Items.FindByText("<--select-->").Selected = true;
                    ddlptigen.ClearSelection();
                    ddlptigen.Items.FindByText("<--select-->").Selected = true;
                    HiddenField2.Value = "";
                }


                else
                {
                    //Update Query Call...........
                    bool UpdateResult = ser.UpdatePaperTypes(txtptid.Text, txtptname.Text, Convert.ToInt16(txtptweight.Text), txtptsize.Text, Convert.ToDouble(txtptcostpersheet.Text), txtptstatus.Text, ptused247, txtptfontfinish.Text, txtptbackfinish.Text, ptDefault, txtptcolor.Text, ptNexPRESS, ptIgen,catName);
                    if (UpdateResult)
                    {
                        lblMessagePTYPE.Visible = true;
                        lblMessagePTYPE.ForeColor = Color.Green;
                        lblMessagePTYPE.Text = "PAPER TYPE VALUES UPDATED SUCCESSFULLY!";
                        fillgrodPaperType();
                        txtptid.Text = "";
                        txtptname.Text = "";
                        txtptweight.Text = "";
                        txtptsize.Text = "";
                        txtptcostpersheet.Text = "";
                        txtptstatus.Text = "";
                        txtptfontfinish.Text = "";
                        txtptbackfinish.Text = "";
                        txtptcolor.Text = "";
                        ddlptdefault.ClearSelection();
                        ddlptdefault.Items.FindByText("<--select-->").Selected = true;
                        ddlptnexpress.ClearSelection();
                        ddlptnexpress.Items.FindByText("<--select-->").Selected = true;
                        ddlptusedby247.ClearSelection();
                        ddlptusedby247.Items.FindByText("<--select-->").Selected = true;
                        ddlptigen.ClearSelection();
                        ddlptigen.Items.FindByText("<--select-->").Selected = true;
                        HiddenField2.Value = "";
                    }
                    else
                    {
                        lblMessagePTYPE.Visible = true;
                        lblMessagePTYPE.ForeColor = Color.Red;
                        lblMessagePTYPE.Text = "Updation failed please try again.";

                    }
                    HiddenField2.Value = "";
                }

            }
            txtptid.Text = "";
            txtptname.Text = "";
            txtptweight.Text = "";
            txtptsize.Text = "";
            txtptcostpersheet.Text = "";
            txtptstatus.Text = "";
            txtptfontfinish.Text = "";
            txtptbackfinish.Text = "";
            txtptcolor.Text = "";
            ddlptdefault.ClearSelection();
            ddlptdefault.Items.FindByText("<--select-->").Selected = true;
            ddlptnexpress.ClearSelection();
            ddlptnexpress.Items.FindByText("<--select-->").Selected = true;
            ddlptusedby247.ClearSelection();
            ddlptusedby247.Items.FindByText("<--select-->").Selected = true;
            ddlptigen.ClearSelection();
            ddlptigen.Items.FindByText("<--select-->").Selected = true;
            HiddenField2.Value = "";
        }

        protected void btndelete_Click1(object sender, EventArgs e)
        {
            if (HiddenField2.Value != "EditPAPERTYPE")
            {

                lblMessagePTYPE.Visible = true;
                lblMessagePTYPE.ForeColor = Color.Red;
                lblMessagePTYPE.Text = "PLEASE SELECT THE RECORD FROM TABLE!";
            }
            else
            {


                bool deletePaperType = ser.RemovePaperType(GrdvwPTID.Rows[Convert.ToInt16(Session["INDEX"].ToString())].Cells[1].Text);
                if (deletePaperType)
                {
                    lblMessagePTYPE.Visible = true;
                    lblMessagePTYPE.ForeColor = Color.Green;
                    lblMessagePTYPE.Text = "PAPER TYPE VALUES HAS BEEN DELETED SUCCESSFULLY!";
                    HiddenField2.Value = "";
                }
                else
                {
                    lblMessagePTYPE.Visible = true;
                    lblMessagePTYPE.ForeColor = Color.Red;
                    lblMessagePTYPE.Text = "No match found !";
                    HiddenField2.Value = "";
                }
                // ClearPaperType();
                txtptid.Text = "";
                txtptname.Text = "";
                txtptweight.Text = "";
                txtptsize.Text = "";
                txtptcostpersheet.Text = "";
                txtptstatus.Text = "";
                txtptfontfinish.Text = "";
                txtptbackfinish.Text = "";
                txtptcolor.Text = "";
                ddlptdefault.ClearSelection();
                ddlptdefault.Items.FindByText("<--select-->").Selected = true;
                ddlptnexpress.ClearSelection();
                ddlptnexpress.Items.FindByText("<--select-->").Selected = true;
                ddlptusedby247.ClearSelection();
                ddlptusedby247.Items.FindByText("<--select-->").Selected = true;
                ddlptigen.ClearSelection();
                ddlptigen.Items.FindByText("<--select-->").Selected = true;
                fillgrodPaperType();

            }

        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            ClearPaperType();
        }
    }
}

我有上面的代码,因为在Page_lode事件中它将值绑定到dropedown但是当我要插入但是它没有插入而没有显示到grideview但是在dropedown中所有值都绑定了如何插入grideview?

1 个答案:

答案 0 :(得分:0)

执行此操作的正确方法是在将gridbiew绑定到数据源之前保留全局变量作为数据源(您自己构建的数据表),以及当某人从下拉列表中选择某些内容时(或者您想要的任何事件) ,将内容/插入更新到全局变量,然后将其重新绑定到网格视图。

这将做你想要的。