简体   繁体   English

将新行添加到Asp.net网格视图,而无需刷新以前的行值

[英]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 这是我的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: 有3种步骤可以做到:

  1. Get data from GridView and save to datatable 从GridView获取数据并保存到数据表
  2. Add new row to datatable and save datatable to viewstate 将新行添加到数据表并将数据表保存到viewstate
  3. Bind datatable to gridview 将数据表绑定到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 现在订阅Gridview RowBound事件

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

also add "RowIndex" Attribute to any of your gridview controls 还将“ RowIndex”属性添加到任何gridview控件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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