简体   繁体   中英

Add new rows to Asp.net grid view without refresh previous rows values

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 在此处输入图片说明

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 
    }
}
 }

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM