Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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"));
    }
}
Posted
v3
Comments
Krunal Rohit 24-Mar-14 11:13am    
i markup, give this attribute for the dropdowns,
AutoPostBack = false;

-KR
Too much of code. Have you debugged the code? What are your findings?
Christian Amado 24-Mar-14 17:59pm    
Yes, too much code here. I have a headache now. Do you use AJAX or not? What about IsPostBack property?
Are you asking me or the OP?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900