当新行动态添加到ASP.Net GridView时,无法保持前导数据DropDownList [英] Unable to stay prvious data DropDownList when new row is added dynamically to ASP.Net GridView

查看:54
本文介绍了当新行动态添加到ASP.Net GridView时,无法保持前导数据DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GridView 中使用3 DropDownLists .. i binded DropDownList 数据库中的值 GridView 第一个 DropDownList 工作正常..

但是选择基于第一个 DropDownList 自动绑定第二个它也工作正常



*当我点击添加新行按钮。 .2nd和3rd DropDownList的之前的数据丢失..我可以做的请给我解决方案下面我的.cs代码

i am using 3 DropDownLists in GridView.. i binded DropDownList value from database in GridView first DropDownList working fine..
but selected based on 1st DropDownList automatically bind second its also work fine

* when i click add new row button..2nd and 3rd DropDownList's previous data lose..how i can do please give me the solution below my .cs code

public partial class WebForm1 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    string str = ConfigurationManager.ConnectionStrings["sugarcon"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {          
           FirstGridViewRow();
        }
    }
    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Head_Name", typeof(string)));
        dt.Columns.Add(new DataColumn("ARTICLE_NAME", typeof(string)));
        dt.Columns.Add(new DataColumn("Descriptoin", typeof(string)));
        dt.Columns.Add(new DataColumn("BIN_NO", typeof(string)));
        dt.Columns.Add(new DataColumn("Unit_Type", typeof(string)));
        dt.Columns.Add(new DataColumn("Received_qty", typeof(string)));
        dt.Columns.Add(new DataColumn("Rejected_qty", typeof(string)));
        dt.Columns.Add(new DataColumn("Accepted_qty", typeof(string)));
        dt.Columns.Add(new DataColumn("rate", typeof(string)));
        dt.Columns.Add(new DataColumn("Value", typeof(string)));
        dt.Columns.Add(new DataColumn("vat", typeof(string)));
        dt.Columns.Add(new DataColumn("cess", typeof(string)));
        dt.Columns.Add(new DataColumn("education_cess", typeof(string)));
        dt.Columns.Add(new DataColumn("higher_education_cess", typeof(string)));
        dt.Columns.Add(new DataColumn("Total", typeof(string)));
        dt.Columns.Add(new DataColumn("Inspection_remarks", typeof(string)));
    
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Head_Name"] = string.Empty;
        dr["ARTICLE_NAME"] = string.Empty;
        dr["Descriptoin"] = string.Empty;
        dr["BIN_NO"] = string.Empty;
        dr["Unit_Type"] = string.Empty;
        dr["Received_qty"] = string.Empty;
        dr["Rejected_qty"] = string.Empty;
        dr["Accepted_qty"] = string.Empty;
        dr["rate"] = string.Empty;
        dr["Value"] = string.Empty;
        dr["vat"] = string.Empty;
        dr["cess"] = string.Empty;
        dr["education_cess"] = string.Empty;
        dr["higher_education_cess"] = string.Empty;
        dr["Total"] = string.Empty;
        dr["Inspection_remarks"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;

        GridView.DataSource = dt;
        GridView.DataBind();
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList ddlhead = (DropDownList)GridView.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList ddlartical = (DropDownList)GridView.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    DropDownList ddldesc = (DropDownList)GridView.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
                    Label lbbinNo = (Label)GridView.Rows[rowIndex].Cells[4].FindControl("binLabel");
                    DropDownList ddlunit = (DropDownList)GridView.Rows[rowIndex].Cells[5].FindControl("DropDownList4");
                    TextBox Txtrecive = (TextBox)GridView.Rows[rowIndex].Cells[6].FindControl("TextBox1");
                    TextBox Txtreject = (TextBox)GridView.Rows[rowIndex].Cells[7].FindControl("TextBox2");
                    TextBox TXtaccept = (TextBox)GridView.Rows[rowIndex].Cells[8].FindControl("TextBox3");
                    TextBox Txtrate = (TextBox)GridView.Rows[rowIndex].Cells[9].FindControl("TextBox4");
                    TextBox Txtval = (TextBox)GridView.Rows[rowIndex].Cells[10].FindControl("TextBox5");
                    TextBox Txtvat = (TextBox)GridView.Rows[rowIndex].Cells[11].FindControl("TextBox6");
                    TextBox Txtcess = (TextBox)GridView.Rows[rowIndex].Cells[12].FindControl("TextBox7");
                    TextBox Txteducation = (TextBox)GridView.Rows[rowIndex].Cells[13].FindControl("TextBox8");
                    TextBox Txthighedu = (TextBox)GridView.Rows[rowIndex].Cells[14].FindControl("TextBox9");
                    TextBox Txttotal = (TextBox)GridView.Rows[rowIndex].Cells[15].FindControl("TextBox10");
                    TextBox Txtremarks = (TextBox)GridView.Rows[rowIndex].Cells[16].FindControl("TextBox11");

                    ddlhead.Text = dt.Rows[i]["Head_Name"].ToString();
                    ddlartical.Text = dt.Rows[i]["ARTICLE_NAME"].ToString();
                    ddldesc.Text = dt.Rows[i]["Descriptoin"].ToString();
                    lbbinNo.Text = dt.Rows[i]["BIN_NO"].ToString();
                    ddlunit.Text = dt.Rows[i]["Unit_Type"].ToString();
                    Txtrecive.Text = dt.Rows[i]["Received_qty"].ToString();
                    Txtreject.Text = dt.Rows[i]["Rejected_qty"].ToString();
                    TXtaccept.Text = dt.Rows[i]["Accepted_qty"].ToString();
                    Txtrate.Text = dt.Rows[i]["rate"].ToString();
                    Txtval.Text = dt.Rows[i]["Value"].ToString();
                    Txtvat.Text = dt.Rows[i]["vat"].ToString();
                    Txtcess.Text = dt.Rows[i]["cess"].ToString();
                    Txteducation.Text = dt.Rows[i]["education_cess"].ToString();
                    Txthighedu.Text = dt.Rows[i]["higher_education_cess"].ToString();
                    Txttotal.Text = dt.Rows[i]["Total"].ToString();
                    Txtremarks.Text = dt.Rows[i]["Inspection_remarks"].ToString();
                    rowIndex++;
                }
            }
        }
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList ddlhead = (DropDownList)GridView.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList ddlartical = (DropDownList)GridView.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    DropDownList ddldesc = (DropDownList)GridView.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
                    Label lbbinNo = (Label)GridView.Rows[rowIndex].Cells[4].FindControl("binLabel");
                    DropDownList ddlunit = (DropDownList)GridView.Rows[rowIndex].Cells[5].FindControl("DropDownList4");
                    TextBox Txtrecive = (TextBox)GridView.Rows[rowIndex].Cells[6].FindControl("TextBox1");
                    TextBox Txtreject = (TextBox)GridView.Rows[rowIndex].Cells[7].FindControl("TextBox2");
                    TextBox TXtaccept = (TextBox)GridView.Rows[rowIndex].Cells[8].FindControl("TextBox3");
                    TextBox Txtrate = (TextBox)GridView.Rows[rowIndex].Cells[9].FindControl("TextBox4");
                    TextBox Txtval = (TextBox)GridView.Rows[rowIndex].Cells[10].FindControl("TextBox5");
                    TextBox Txtvat = (TextBox)GridView.Rows[rowIndex].Cells[11].FindControl("TextBox6");
                    TextBox Txtcess = (TextBox)GridView.Rows[rowIndex].Cells[12].FindControl("TextBox7");
                    TextBox Txteducation = (TextBox)GridView.Rows[rowIndex].Cells[13].FindControl("TextBox8");
                    TextBox Txthighedu = (TextBox)GridView.Rows[rowIndex].Cells[14].FindControl("TextBox9");
                    TextBox Txttotal = (TextBox)GridView.Rows[rowIndex].Cells[15].FindControl("TextBox10");
                    TextBox Txtremarks = (TextBox)GridView.Rows[rowIndex].Cells[16].FindControl("TextBox11");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Head_Name"] = ddlhead.Text;
                    dtCurrentTable.Rows[i - 1]["ARTICLE_NAME"] = ddlartical.Text;
                    dtCurrentTable.Rows[i - 1]["Descriptoin"] = ddldesc.Text;
                    dtCurrentTable.Rows[i - 1]["BIN_NO"] = lbbinNo.Text;
                    dtCurrentTable.Rows[i - 1]["Unit_Type"] = ddlunit.Text;
                    dtCurrentTable.Rows[i - 1]["Received_qty"] = Txtrecive.Text;
                    dtCurrentTable.Rows[i - 1]["Rejected_qty"] = Txtreject.Text;
                    dtCurrentTable.Rows[i - 1]["Accepted_qty"] = TXtaccept.Text;
                    dtCurrentTable.Rows[i - 1]["rate"] = Txtrate.Text;
                    dtCurrentTable.Rows[i - 1]["Value"] = Txtval.Text;
                    dtCurrentTable.Rows[i - 1]["vat"] = Txtvat.Text;
                    dtCurrentTable.Rows[i - 1]["cess"] = Txtcess.Text;
                    dtCurrentTable.Rows[i - 1]["education_cess"] = Txteducation.Text;
                    dtCurrentTable.Rows[i - 1]["higher_education_cess"] = Txthighedu.Text;
                    dtCurrentTable.Rows[i - 1]["Total"] = Txttotal.Text;
                    dtCurrentTable.Rows[i - 1]["Inspection_remarks"] = Txtremarks.Text;
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GridView.DataSource = dtCurrentTable;
                GridView.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }
    private void SetRowData()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList ddlhead = (DropDownList)GridView.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList ddlartical = (DropDownList)GridView.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    DropDownList ddldesc = (DropDownList)GridView.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
                    Label lbbinNo = (Label)GridView.Rows[rowIndex].Cells[4].FindControl("binLabel");
                    DropDownList ddlunit = (DropDownList)GridView.Rows[rowIndex].Cells[5].FindControl("DropDownList4");
                    TextBox Txtrecive = (TextBox)GridView.Rows[rowIndex].Cells[6].FindControl("TextBox1");
                    TextBox Txtreject = (TextBox)GridView.Rows[rowIndex].Cells[7].FindControl("TextBox2");
                    TextBox TXtaccept = (TextBox)GridView.Rows[rowIndex].Cells[8].FindControl("TextBox3");
                    TextBox Txtrate = (TextBox)GridView.Rows[rowIndex].Cells[9].FindControl("TextBox4");
                    TextBox Txtval = (TextBox)GridView.Rows[rowIndex].Cells[10].FindControl("TextBox5");
                    TextBox Txtvat = (TextBox)GridView.Rows[rowIndex].Cells[11].FindControl("TextBox6");
                    TextBox Txtcess = (TextBox)GridView.Rows[rowIndex].Cells[12].FindControl("TextBox7");
                    TextBox Txteducation = (TextBox)GridView.Rows[rowIndex].Cells[13].FindControl("TextBox8");
                    TextBox Txthighedu = (TextBox)GridView.Rows[rowIndex].Cells[14].FindControl("TextBox9");
                    TextBox Txttotal = (TextBox)GridView.Rows[rowIndex].Cells[15].FindControl("TextBox10");
                    TextBox Txtremarks = (TextBox)GridView.Rows[rowIndex].Cells[16].FindControl("TextBox11");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Head_Name"] = ddlhead.Text;
                    dtCurrentTable.Rows[i - 1]["ARTICLE_NAME"] = ddlartical.Text;
                    dtCurrentTable.Rows[i - 1]["Descriptoin"] = ddldesc.Text;
                    dtCurrentTable.Rows[i - 1]["BIN_NO"] = lbbinNo.Text;
                    dtCurrentTable.Rows[i - 1]["Unit_Type"] = ddlunit.Text;
                    dtCurrentTable.Rows[i - 1]["Received_qty"] = Txtrecive.Text;
                    dtCurrentTable.Rows[i - 1]["Rejected_qty"] = Txtreject.Text;
                    dtCurrentTable.Rows[i - 1]["Accepted_qty"] = TXtaccept.Text;
                    dtCurrentTable.Rows[i - 1]["rate"] = Txtrate.Text;
                    dtCurrentTable.Rows[i - 1]["Value"] = Txtval.Text;
                    dtCurrentTable.Rows[i - 1]["vat"] = Txtvat.Text;
                    dtCurrentTable.Rows[i - 1]["cess"] = Txtcess.Text;
                    dtCurrentTable.Rows[i - 1]["education_cess"] = Txteducation.Text;
                    dtCurrentTable.Rows[i - 1]["higher_education_cess"] = Txthighedu.Text;
                    dtCurrentTable.Rows[i - 1]["Total"] = Txttotal.Text;
                    dtCurrentTable.Rows[i - 1]["Inspection_remarks"] = Txtremarks.Text;
                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
                //grvStudentDetails.DataSource = dtCurrentTable;
                //grvStudentDetails.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }
    protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                GridView.DataSource = dt;
                GridView.DataBind();

                for (int i = 0; i < GridView.Rows.Count - 1; i++)
                {
                    GridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddlhead = (DropDownList)e.Row.FindControl("DropDownList1");
            con = new SqlConnection(str);
            con.Open();
          cmd = new SqlCommand(" select distinct TMAST_HEAD_NAME from TMAST",con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            ddlhead.DataSource = dt;
            ddlhead.DataTextField = "TMAST_HEAD_NAME";
            ddlhead.DataValueField = "TMAST_HEAD_NAME";
            ddlhead.DataBind();
            ddlhead.Items.Insert(0,new ListItem("-select-","0"));
                       }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Here you can find the row which row's dropdown is fired
        GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
        // Now convert sender as dropdown which is fired and you can get all the properties   of the dropdown
        DropDownList ddlhead = (DropDownList)sender;

         DropDownList ddlartical = (DropDownList)gvr.Cells[2].FindControl("DropDownList2");
        con = new SqlConnection(str);
        con.Open();
        cmd = new SqlCommand("select distinct TMAST_ARTICLE_NAME from TMAST where TMAST_HEAD_NAME='" + ddlhead.SelectedValue + "'", con);
       
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        ddlartical.DataSource = dt;
        ddlartical.DataTextField = "TMAST_ARTICLE_NAME";
        ddlartical.DataValueField = "TMAST_ARTICLE_NAME";
        ddlartical.DataBind();
        ddlartical.Items.Insert(0, new ListItem("-select-", "0"));

        if (ViewState["currentKeyValues"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
          //  List<string> keyValues = ViewState["currentKeyValues"] as List<string>;
            dtCurrentTable.Rows.Add(ddlartical.SelectedValue);
            ViewState["currentKeyValues"] = dtCurrentTable;
        }
        else
        {
            //List<string> keyValues = new List<string>();
            //keyValues.Add(ddlartical.SelectedValue);
            //ViewState["currentKeyValues"] = keyValues;
        }    
       }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlarticale = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddlarticale.NamingContainer;
        //DataGridItem row = (DataGridItem)ddlLabTest.NamingContainer;
        DropDownList ddlartical = (DropDownList)row.FindControl("DropDownList2");

        //int idx = row.RowIndex;
        // TextBox txtgroup = (TextBox)row.Cells[12].FindControl("TextBox8");
        DropDownList ddldescription = (DropDownList)row.Cells[3].FindControl("DropDownList3");

        con = new SqlConnection(str);
        con.Open();
        cmd = new SqlCommand("select distinct TMAST_DESCRIPTION from TMAST where TMAST_ARTICLE_NAME='" + ddlartical.SelectedItem.Value + "'", con);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        ddldescription.DataSource = dt;
        ddldescription.DataTextField = "TMAST_DESCRIPTION";
        ddldescription.DataValueField = "TMAST_DESCRIPTION";
        ddldescription.DataBind();
        ddldescription.Items.Insert(0, new ListItem("-select-", "0"));
    }
}

推荐答案

这篇关于当新行动态添加到ASP.Net GridView时,无法保持前导数据DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆