如何在asp.net中将DropDownList选择值插入GridView?

时间:2014-05-06 05:06:49

标签: c# asp.net gridview

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?和博士[&#34; Cat_Name&#34;] = PaperInfo [i] .Cat_Id;只有它没有取值,但在我的数据库中,cat_id值存在。

2 个答案:

答案 0 :(得分:0)

对不起朋友。但我已经看到你的代码不正确的方式,听到了解。 但是我明白了,你首先绑定gridview,因为你正在调用方法 ser.GetALLPaperTypeInfo(); 请检查 PaperInfo [i] .Cat_Id; 正在返回的内容。 Cat_ID Cat_Name ? 第二,你必须在页面lode事件中提供 ddlcatid.DataValueField 。 你应该在这里提供 ser.InsertPaperType()方法。想看看插入时究竟输入什么错误? 您正在将Cat_Name作为布尔插入,但在绑定网格视图时,您正在定义 dt.Columns.Add(new DataColumn(“Cat_Name”,typeof(string))); < / p>

请提供正确的代码,仅限具体。

答案 1 :(得分:0)

您应该尝试在按钮的btnsubmit_Click(对象发送者,EventArgs e)事件上绑定gridview而不是页面加载,并确保根据您的要求将autogeneratecolumns设置为true / false。