0

Hi I have a grid view with two columns text box and drop down list when I add values and click "ADD" button I want to add new row with Previous values, I do it but my previous values refresh. Please help me. This is my aspx

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownListAddress" runat="server" AutoPostBack="true">
                            <asp:ListItem Text="Select" Value="0"> </asp:ListItem>
                            <asp:ListItem Text="Address1" Value="1"> </asp:ListItem>
                            <asp:ListItem Text="Address2" Value="2"> </asp:ListItem>

                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
    <div>
    <asp:Button ID="ButtonADD" runat="server" Text="Add" OnClick="ButtonADD_Click" />
    </div>
</form>

And this is my output enter image description here

This is my CodeBehind

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;

        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Address", typeof(string)));

        dr = dt.NewRow();

        dr["Name"] = string.Empty;
        dr["Address"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["StudentTable"] = dt;

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

    protected void ButtonADD_Click(object sender, EventArgs e)
    {
        //Add Rows 
    }
}
 }
3
  • Are you calling SetInitialRow() in ButtonADD_Click? You need to extract values from GridView before postback and save them in DataTable and rebind them. Commented Aug 23, 2015 at 7:08
  • no I only called SetInitialRow() in page load. Can you send me example please Commented Aug 23, 2015 at 7:28
  • check this link out very simple code Add new rows dynamically in a grid view Commented Jun 1, 2017 at 3:34

1 Answer 1

0

There are 3 stpes to do it:

  1. Get data from GridView and save to datatable
  2. Add new row to datatable and save datatable to viewstate
  3. Bind datatable to gridview

    private void SaveGridViewDataIntoDataTable()
    {
        DataTable StudentTable = ViewState["StudentTable"] as DataTable;
    
        foreach (GridViewRow row in gvStudent.Rows)
        {
            //get ddl value
            DropDownList  DropDownListAddress = row.FindControl("DropDownListAddress") as DropDownList;
            StudentTable.Rows[row.RowIndex]["Address"] = DropDownListAddress.SelectedValue;
    
            //get name from textbox
            TextBox TextBoxName = row.FindControl("TextBoxName") as TextBox;
            StudentTable.Rows[row.RowIndex]["Name"] = TextBoxName.Text;
        }
    
        ViewState["StudentTable"] = StudentTable;
    }
    
    private void AddNewRowToGridView()
    {
        SaveGridViewDataIntoDataTable();
    
        DataTable StudentTable = ViewState["StudentTable"] as DataTable;
        StudentTable.Rows.Add("Name", "Select");
    
        gvStudent.DataSource = StudentTable;
        gvStudent.DataBind();
    }
    

now subscribe to Gridview RowBound event

<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvStudent_RowDataBound">

also add "RowIndex" Attribute to any of your gridview controls

<asp:TextBox ID="TextBoxName" runat="server" RowIndex='<%# Container.DisplayIndex %>'></asp:TextBox>

Code behind:

protected void gvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //get name from textbox
        TextBox TextBoxName = row.FindControl("TextBoxName") as TextBox;

        //get ddl value
        DropDownList DropDownListAddress = row.FindControl("DropDownListAddress") as DropDownList;

        //get rowindex
        int RowIndex = Convert.ToInt32(TextBoxName.Attributes["RowIndex"]);

        //get datatable stored in viewstate
        DataTable StudentTable = ViewState["StudentTable"] as DataTable;

        //assign values to controls
        TextBoxName.Text = StudentTable.Rows[RowIndex]["Name"].ToString();
        DropDownListAddress.SelectedValue = StudentTable.Rows[RowIndex]["Address"].ToString();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.