【问题标题】:Add new rows to Asp.net grid view without refresh previous rows values将新行添加到 Asp.net 网格视图而不刷新以前的行值
【发布时间】:2015-08-23 06:38:47
【问题描述】:

嗨,当我添加值并单击“添加”按钮时,我有一个带有两列文本框和下拉列表的网格视图,我想添加具有先前值的新行, 我这样做了,但我以前的值会刷新。请帮我。 这是我的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>

这是我的输出

这是我的代码隐藏

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

【问题讨论】:

  • 你在ButtonADD_Click中打电话给SetInitialRow()吗?您需要在回发之前从GridView 中提取值并将它们保存在DataTable 中并重新绑定它们。
  • 不,我只在页面加载时调用了 SetInitialRow()。你能给我发个例子吗
  • 查看这个链接非常简单的代码Add new rows dynamically in a grid view

标签: c# asp.net gridview


【解决方案1】:

有 3 个步骤可以做到:

  1. 从 GridView 获取数据并保存到数据表中
  2. 向数据表添加新行并将数据表保存到视图状态
  3. 将数据表绑定到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();
    }
    

现在订阅 Gridview RowBound 事件

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

还将“RowIndex”属性添加到您的任何 gridview 控件

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

后面的代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 2016-10-02
    • 2013-05-24
    相关资源
    最近更新 更多