Click here to Skip to main content
15,918,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a dynamic gridview in which each row have a new dropdownlist so i want to set dropsownlist for 1st row index will be 1, for 2nd row index will be 2 and so on...

my code is as follow please help.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FirstGridViewRow();
        FirstGridViewRowoutput();
        txtDate.Text = DateTime.Now.ToString("MM/dd/yyyy").Replace("-", "/");
        AddNewRow();
        AddNewRow();
        AddNewRowoutput();
        //grddtl.Rows[0].Cells[1].
        }
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }

    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Col1", typeof(string)));
        dt.Columns.Add(new DataColumn("Col2", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Col1"] = string.Empty;
        dr["Col2"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;

        grddtl.DataSource = dt;
        grddtl.DataBind();
    }

    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 TextBoxName =                      (DropDownList)grddtl.Rows[rowIndex].Cells[1].FindControl("ddlitem");
                    TextBox TextBoxQty =
                      (TextBox)grddtl.Rows[rowIndex].Cells[2].FindControl("txtqty");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    
                    dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;
                    rowIndex++;
                }
                
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                grddtl.DataSource = dtCurrentTable;
                grddtl.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }

    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 TextBoxName = (DropDownList)grddtl.Rows[rowIndex].Cells[1].FindControl("ddlitem");
                    TextBox TextBoxQty = (TextBox)grddtl.Rows[rowIndex].Cells[2].FindControl("txtqty");

                    TextBoxName.SelectedValue = dt.Rows[i]["Col1"].ToString();
                    TextBoxQty.Text = dt.Rows[i]["Col2"].ToString();
                    rowIndex++;
                }
            }
        }
    }
    protected void grddtl_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;
                grddtl.DataSource = dt;
                grddtl.DataBind();

                for (int i = 0; i < grddtl.Rows.Count - 1; i++)
                {
                    grddtl.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                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 TextBoxName =(DropDownList)grddtl.Rows[rowIndex].Cells[1].FindControl("ddlitem");
                    TextBox TextBoxQty = (TextBox)grddtl.Rows[rowIndex].Cells[2].FindControl("txtqty");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;

                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
                //grvStudentDetails.DataSource = dtCurrentTable;
                //grvStudentDetails.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        //SetPreviousData();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            SetRowData();
            SetRowDataoutput();
            DataTable table = ViewState["CurrentTable"] as DataTable;
            DataTable tableoutput = ViewState["CurrentTableoutput"] as DataTable;
            if (table != null && tableoutput !=null)
            {
                conn.Open();
                SqlCommand cmdhdr = new SqlCommand("insert into productionhdr(date,note) values ('" + txtDate.Text + "','" + txtNote.Text + "') ", conn);
                SqlDataAdapter dahdr = new SqlDataAdapter(cmdhdr);
                cmdhdr.ExecuteNonQuery();
                SqlCommand getID = new SqlCommand("Select max(id) from productionhdr", conn);
                SqlDataAdapter da = new SqlDataAdapter(getID);
                DataSet ds = new DataSet();
                da.Fill(ds);

                //if (ds.Tables[0].Rows[0][0] == DBNull.Value)
                //{
                //    maxrowid = 1;
                //    //int id = (int)ds.Tables[0].Rows[0][0];
                //}
                //else
                //{
                    maxrowid = (int)ds.Tables[0].Rows[0][0];
                //}
                //if (maxrowid > 1)
                //    maxrowid = maxrowid + 1;

                foreach (DataRow row in table.Rows)
                {
                    string txtName = row.ItemArray[1] as string;
                    string txtQty = row.ItemArray[2] as string;

                    if (txtName != null || txtQty != null)
                    {
                        SqlCommand cmddtl = new SqlCommand("insert into productiondtl(pid,item,qty,type) values(" + maxrowid + ",'" + txtName + "'," + txtQty + ",'I')", conn);                        
                        SqlDataAdapter dadtl = new SqlDataAdapter(cmddtl);
                        //DataSet ds=new DataSet();
                        //da.Fill(ds);                        
                        cmddtl.ExecuteNonQuery();                       
                    }
                }
                foreach (DataRow row in tableoutput.Rows)
                {
                    string txtName = row.ItemArray[1] as string;
                    string txtQty = row.ItemArray[2] as string;

                    if (txtName != null || txtQty != null)
                    {
                        SqlCommand cmddtloutput = new SqlCommand("insert into productiondtl(pid,item,qty,type) values(" + maxrowid + ",'" + txtName + "'," + txtQty + ",'O')", conn);
                        SqlDataAdapter dadtloutput = new SqlDataAdapter(cmddtloutput);
                        //DataSet ds=new DataSet();
                        //da.Fill(ds);                        
                        cmddtloutput.ExecuteNonQuery();
                    }
                }
                lblmsg.Text = "Data Saved Sucessfully...!!!!";
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the DropDownList in the Row
            DropDownList ddlItem = (e.Row.FindControl("ddlitem") as DropDownList);
            conn.Open();
            string query = "SELECT id,name FROM itemmst";
            SqlCommand cmd = new SqlCommand(query,conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds=new DataSet();
            da.Fill(ds);

            ddlItem.DataSource = ds;
            ddlItem.DataTextField = "name";
            ddlItem.DataValueField = "id";
            ddlItem.DataBind();

            //Add Default Item in the DropDownList
            ddlItem.Items.Insert(0, new ListItem("Select Item"));
            conn.Close();

            //Select the Country of Customer in DropDownList
        }
    }

    //for output
    protected void ButtonAddoutput_Click(object sender, EventArgs e)
    {
        AddNewRowoutput();
    }
    private void FirstGridViewRowoutput()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumberoutput", typeof(string)));
        dt.Columns.Add(new DataColumn("Col1", typeof(string)));
        dt.Columns.Add(new DataColumn("Col2", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumberoutput"] = 1;
        dr["Col1"] = string.Empty;
        dr["Col2"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["CurrentTableoutput"] = dt;

        grddtloutput.DataSource = dt;
        grddtloutput.DataBind();
    }
    private void AddNewRowoutput()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTableoutput"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableoutput"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList TextBoxName =
(DropDownList)grddtloutput.Rows[rowIndex].Cells[1].FindControl("ddlitemoutput");

                    TextBox TextBoxQty =
(TextBox)grddtloutput.Rows[rowIndex].Cells[2].FindControl("txtqtyoutput");

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

                    dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTableoutput"] = dtCurrentTable;

                grddtloutput.DataSource = dtCurrentTable;
                grddtloutput.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousDataoutput();
    }
    private void SetPreviousDataoutput()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTableoutput"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTableoutput"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList TextBoxName = (DropDownList)grddtloutput.Rows[rowIndex].Cells[1].FindControl("ddlitemoutput");
                    TextBox TextBoxQty = (TextBox)grddtloutput.Rows[rowIndex].Cells[2].FindControl("txtqtyoutput");

                    TextBoxName.SelectedValue = dt.Rows[i]["Col1"].ToString();
                    TextBoxQty.Text = dt.Rows[i]["Col2"].ToString();
                    rowIndex++;
                }
            }
        }
    }
    protected void grddtloutput_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowDataoutput();
        if (ViewState["CurrentTableoutput"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTableoutput"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTableoutput"] = dt;
                grddtloutput.DataSource = dt;
                grddtloutput.DataBind();

                for (int i = 0; i < grddtloutput.Rows.Count - 1; i++)
                {
                    grddtloutput.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousDataoutput();
            }
        }
    }
    private void SetRowDataoutput()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTableoutput"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableoutput"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList TextBoxName = (DropDownList)grddtloutput.Rows[rowIndex].Cells[1].FindControl("ddlitemoutput");
                    TextBox TextBoxQty = (TextBox)grddtloutput.Rows[rowIndex].Cells[2].FindControl("txtqtyoutput");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumberoutput"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxQty.Text;

                    rowIndex++;
                }

                ViewState["CurrentTableoutput"] = dtCurrentTable;
                //grvStudentDetails.DataSource = dtCurrentTable;
                //grvStudentDetails.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        //SetPreviousData();
    }
   
    protected void OnRowDataBoundoutput(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the DropDownList in the Row
            DropDownList ddlItem = (e.Row.FindControl("ddlitemoutput") as DropDownList);
            conn.Open();
            string query = "SELECT id,name FROM itemmst";
            SqlCommand cmd = new SqlCommand(query, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ddlItem.DataSource = ds;
            ddlItem.DataTextField = "name";
            ddlItem.DataValueField = "id";
            ddlItem.DataBind();

            //Add Default Item in the DropDownList
            ddlItem.Items.Insert(0, new ListItem("Select Item"));
            conn.Close();

            //Select the Country of Customer in DropDownList
        }
    }
Posted
Updated 22-Jan-14 3:33am
v2

1 solution

replace your onRowdatabound event with below event
may it will help you

C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
		int index = e.Row.RowIndex;
            //Find the DropDownList in the Row
            DropDownList ddlItem = (e.Row.FindControl("ddlitem") as DropDownList);
            conn.Open();
            string query = "SELECT id,name FROM itemmst";
            SqlCommand cmd = new SqlCommand(query,conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds=new DataSet();
            da.Fill(ds);
 
            ddlItem.DataSource = ds;
            ddlItem.DataTextField = "name";
            ddlItem.DataValueField = "id";
            ddlItem.DataBind();
		
 	
            //Add Default Item in the DropDownList
            ddlItem.Items.Insert(0, new ListItem("Select Item"));
            conn.Close();
		ddlItem.SelectedIndex=index;
 
            //Select the Country of Customer in DropDownList
        }
    }
 
Share this answer
 
v2

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