c#将行添加到daatagridview会删除其他行的内容

时间:2018-04-23 12:12:51

标签: c# asp.net datagridview

我正在尝试向datagridview添加行并在新行中编写一些行。任何后续添加都会删除预览行的内容

private void button1_Click(object sender, EventArgs e)
        {
            dataGridView2.RowCount = dataGridView2.RowCount + 1;
            dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[0].Value = "smth";


                dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[1].Value = "something";

        }

2 个答案:

答案 0 :(得分:2)

试试这个

dataGridView2.Rows.Add("Smth","Something");

答案 1 :(得分:2)

您似乎遇到ASP.NET State Management

的问题

触发button1_Click事件后,将创建ASP.NET页面的新实例,并重置dataGridView2的来源。这就是为什么

  

任何后续添加都会删除预览行的内容

我试着用我制作的一些样品来解释。有两个GridView。 : 第一个GridView将保持点击之间的值。

第二个GridView不会保持点击之间的值,可能是您现在面临的问题。

GridViews相等,Page_Load相等,Button Click事件相等。

区别在于persistedList和notPersistedList

我的WebForm1.aspx.cs:

using System;
using System.Collections.Generic;

namespace WebApplication4GridAddRow
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        [Serializable]
        public class SampleClass
        {
            public string PropertyA { get; set; }
            public string PropertyB { get; set; }
        }

        //I will create this in a Session List, to mantain the state.
        //You could use Viewstate (ugly!) too, this is why SamplesClass is Serializable.
        List<SampleClass> persistedList
        {
            get
            {
                return (List<SampleClass>)Session["samplesClass"];
                //return (List<SampleClass>)ViewState["samplesClass"];
            }
            set
            {
                Session.Add("samplesClass", value);
                //ViewState.Add("samplesClass", value);
            }
        }

        List<SampleClass> notPersistedList = new List<SampleClass>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                persistedList = new List<SampleClass> {
                    new SampleClass { PropertyA = "smth", PropertyB = "something" },
                    new SampleClass { PropertyA = "smthA", PropertyB = "somethingA" }
                };

                GridView1.DataSource = persistedList;
                GridView1.DataBind();

                notPersistedList = new List<SampleClass> {
                    new SampleClass { PropertyA = "smth", PropertyB = "something" },
                    new SampleClass { PropertyA = "smthA", PropertyB = "somethingA" }
                };

                GridView2.DataSource = notPersistedList;
                GridView2.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            persistedList.Add(new SampleClass { PropertyA = "smth" + persistedList.Count, PropertyB = "something" + persistedList.Count });

            GridView1.DataSource = persistedList;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            notPersistedList.Add(new SampleClass { PropertyA = "smth" + notPersistedList.Count, PropertyB = "something" + notPersistedList.Count });

            GridView2.DataSource = notPersistedList;
            GridView2.DataBind();
        }
    }
}

和WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4GridAddRow.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Using a persisted list. The values are keeped after the Button1_Click
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </div>
        <br />
        <div>
            Not using a persisted list. The values are lost after the Button2_Click
            <asp:GridView ID="GridView2" runat="server">
            </asp:GridView>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
        </div>
    </form>
</body>
</html>

我在Git https://github.com/guiljs/c-sharp-adding-row-to-daatagridview-deletes-content-of-other-rows.git

中上传了示例

希望它对你有所帮助。