Asp.Net change value of textbox in grid on selected Index changed

10,026

solved it

protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        TextBox txtName = (TextBox)row.FindControl("txtCp");
        txtName.Text = "*****";

    }
Share:
10,026
Muneeb Zulfiqar
Author by

Muneeb Zulfiqar

Software Engineer, web developer, .net developer, c++, mobile applications developer SOreadytohelp

Updated on June 28, 2022

Comments

  • Muneeb Zulfiqar
    Muneeb Zulfiqar almost 2 years

    enter image description here

    I have this grid on which I am dynamically adding controls. Now I want to change the respective value of Cost Price and Sale Price on Products selected index changed event. The problem is I cannot get the specific row. Below the code I have done so far.

        <asp:GridView ID="grdSales" runat="server"
                        ShowFooter="True" AutoGenerateColumns="False"
                        CellPadding="4" CssClass="table table-hover table-striped"
                        GridLines="None" OnRowDeleting="grdSales_RowDeleting" OnRowCommand="grdSales_RowCommand">
                        <Columns>
                            <asp:BoundField DataField="RowNumber" HeaderText="SNo" />
    
                            <asp:TemplateField HeaderText="Products">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlProducts" runat="server"  CssClass="grid-control form-control" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged">
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
    
    
                            <asp:TemplateField HeaderText="Cost Price">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtCp" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                              <asp:TemplateField HeaderText="Sale Price">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtSp" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Total Units">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtUnits" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                                </ItemTemplate>
                                  <FooterStyle HorizontalAlign="Left" />
                                <FooterTemplate>
                                   Total Journal Cost: <asp:Label ID="lblTotalCost" runat="server" Text="0"></asp:Label>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Total Cost">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtTotal" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                                </ItemTemplate>
    
    
    
    
                                 <FooterStyle HorizontalAlign="Left" />
                                <FooterTemplate>
                                    <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CssClass="btn btn-primary" OnClick="ButtonAdd_Click" />
                                </FooterTemplate>
                            </asp:TemplateField>
    
                            <asp:CommandField
                                ShowDeleteButton="True" />
                        </Columns>
    
    
                    </asp:GridView>
    

    and my code behind is

     #region Private Functions
        private void FirstGridViewRow()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    
            dt.Columns.Add(new DataColumn("Products", typeof(string)));
    
            dt.Columns.Add(new DataColumn("CostPrice", typeof(string)));
            dt.Columns.Add(new DataColumn("SalePrice", typeof(string)));
            dt.Columns.Add(new DataColumn("TotalUnits", typeof(string)));
            dt.Columns.Add(new DataColumn("TotalCost", typeof(string)));
            dr = dt.NewRow();
    
            dr["RowNumber"] = 1;
    
            dr["Products"] = string.Empty;
    
            dr["CostPrice"] = string.Empty;
            dr["SalePrice"] = string.Empty;
            dr["TotalUnits"] = string.Empty;
            dr["TotalCost"] = string.Empty;
    
    
            dt.Rows.Add(dr);
    
            ViewState["CurrentTable"] = dt;
    
            grdSales.DataSource = dt;
            grdSales.DataBind();
            AddNewRow();
            SetRowData();
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtOld = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                int rowIndex = Convert.ToInt32(0);
                if (dtOld.Rows.Count > 1)
                {
                    dtOld.Rows.Remove(dt.Rows[rowIndex]);
                    drCurrentRow = dtOld.NewRow();
                    ViewState["CurrentTable"] = dtOld;
                    grdSales.DataSource = dtOld;
                    grdSales.DataBind();
    
                    for (int i = 0; i < grdSales.Rows.Count - 1; i++)
                    {
                        grdSales.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    }
                    SetPreviousData();
                }
            }
    
        }
        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 ddlProducts =
                          (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
    
                        TextBox txtCostPrice =
                          (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                        TextBox txtSalePrice =
                         (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                        TextBox txtUnits =
                         (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                        TextBox txtTotal =
                         (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
    
                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;
    
    
                        dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.SelectedValue;
    
                        dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
                        dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
                        dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
                        dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;
    
    
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;
    
                    grdSales.DataSource = dtCurrentTable;
                    grdSales.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 ddlProducts =
                          (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
    
                        TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                        TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                        TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                        TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
    
    
                        //Added these lines
    
                        ddlProducts.DataValueField = "Key";
                        ddlProducts.DataTextField = "Value";
                        ddlProducts.DataSource = BindProductsDdl();
                        ddlProducts.DataBind();
    
                        //****************
    
                        ddlProducts.SelectedValue = dt.Rows[i]["Products"].ToString();
    
                        txtCostPrice.Text = dt.Rows[i]["CostPrice"].ToString();
                        txtSalePrice.Text = dt.Rows[i]["SalePrice"].ToString();
                        txtUnits.Text = dt.Rows[i]["TotalUnits"].ToString();
                        txtTotal.Text = dt.Rows[i]["TotalCost"].ToString();
                        rowIndex++;
                    }
                }
            }
        }
        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 ddlProducts =
                           (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
    
                        TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                        TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                        TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                        TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
    
                        drCurrentRow = dtCurrentTable.NewRow();
    
                        drCurrentRow["RowNumber"] = i + 1;
    
                        dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.Text;
    
                        dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
                        dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
                        dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
                        dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;
    
                        rowIndex++;
                    }
    
                    ViewState["CurrentTable"] = dtCurrentTable;
                    //grvStudentDetails.DataSource = dtCurrentTable;
                    //grvStudentDetails.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }
            //SetPreviousData();
        }
        private void BindControls()
        {
            BindWarehouseDdl(); BindStaticLists();
    
        }
    
        private Dictionary<int, string> BindProductsDdl()
        {
            Dictionary<int, string> Get = new Dictionary<int, string>();
            for (int i = 0; i < Products.Count; i++)
            {
                int id = Convert.ToInt32(Products[i].id);
                string name = Products[i].Name;
                Get.Add(id, name);
            }
            return Get;
        }
        private void BindStaticLists()
        {
            Classes.CProducts cp = new Classes.CProducts();
            Products = cp.GetAll();
        }
        private void BindWarehouseDdl()
        {
            Dictionary<int, string> Items = new Dictionary<int, string>();
            List<Models.MwareHouse> get = new List<Models.MwareHouse>();
            Classes.CWareHouse cw = new Classes.CWareHouse();
            get = cw.GetAll();
            for (int i = 0; i < get.Count; i++)
            {
                string id = get[i].id;
                string name = get[i].Name;
                Items.Add(Convert.ToInt32(id), name);
    
            }
            ddlWareHouse.DataTextField = "Value";
            ddlWareHouse.DataValueField = "Key";
            ddlWareHouse.DataSource = Items;
            ddlWareHouse.DataBind();
        }
        #endregion
    
        protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
    
        protected void grdSales_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow gr = grdSales.SelectedRow;
    
        }