locked
update agridview with a static value RRS feed

  • Question

  • User-1651858287 posted

    I am trying to update a gridView with a static value form a textbox.

    Any assistance will be greatly appreciated.

    <asp:TemplateField HeaderText="EP" SortExpression="EP" Visible="False">
    <EditItemTemplate>
    <!-- <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("EP") %>'></asp:TextBox> -->
    <%# txtEmailPeriod.Text%>

    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label5" runat="server" Text='<%# Bind("EP") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtEPFooter" runat="server"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    </

    Friday, July 28, 2017 1:03 AM

All replies

  • User2103319870 posted

    <asp:TemplateField HeaderText="EP" SortExpression="EP" Visible="False">
    <EditItemTemplate>
    <!-- <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("EP") %>'></asp:TextBox> -->
    <%# txtEmailPeriod.Text%>

    </EditItemTemplate>

    If you want to update the textbox in EditItemTemplate with a Static value from textbox, you can use the RowDataBound event.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                //Check if row is Edit Mode
                if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
                {
                    //Find the control in EditItemTemplate
                    TextBox txt = e.Row.FindControl("TextBox5") as TextBox;
                    //Assign the value to edited textbox
                    txt.Text = txtEmailPeriod.Text;
                }
            }

    Friday, July 28, 2017 1:47 AM
  • User347430248 posted

    Hi sweetSteal,

    this is another example may help you.

     private void AddNewRowToGrid()
        {
            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++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
     
                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;
     
                        dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
     
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;
     
                    Gridview1.DataSource = dtCurrentTable;
                    Gridview1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }
     
            //Set Previous Data on Postbacks
            SetPreviousData();
        }

    Output:

    Reference:

    Add rows in Gridview from Textbox

    Regards

    Deepak

    Friday, July 28, 2017 8:42 AM
  • User-1651858287 posted

    thanks for you response but its not working for me.

    I have five columns (ID, Duties, Score, comment and EP).

    The ID and EP are hidden from the user.

    When the user tries to Edit the row the ID, Duties, Score, comment works but the 

    EP gets null value.

    I have a txtEmailPeriod.Text which has the EP stored.

    EP should behave like the Id

    Friday, July 28, 2017 9:34 PM
  • User347430248 posted

    Hi sweetSteal,

    can you post your code.

    so that we can see how your code is working.

    we can try to make a test with it and try to modify it to make it work.

    Regards

    Deepak

    Monday, July 31, 2017 6:28 AM