我正在从 gridview 中删除行,但 table 中的所有数据都被删除

I'm deleting rows from gridview but all data in the table is deleted

我想在 Gridview 中动态添加和删除行。添加成功。当我删除该行时,该行被成功删除。但是在其他行中输入的数据被清除。 我想删除行。我还想防止删除以前输入的数据。

Default.aspx

    <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged">
        <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Header 2">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 3">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 4">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ControlStyle-CssClass="row-edit" HeaderText="">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" CommandName="Select" runat="server"><i class="fas fa-times"></i></asp:LinkButton>
                </ItemTemplate>
                <ControlStyle CssClass="row-edit"></ControlStyle>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />

Default.aspx.cs

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();

                    rowIndex++;
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int indis = Gridview1.SelectedIndex;
        GridViewRow satir = Gridview1.Rows[indis];
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow[] _row = dtCurrentTable.Select("RowNumber=" + satir.Cells[0].Text);
        foreach (DataRow row in _row)
        {
            row.Delete();
        }
        dtCurrentTable.AcceptChanges();
        ViewState["CurrentTable"] = dtCurrentTable;
        Gridview1.DataSource = dtCurrentTable;
        Gridview1.DataBind();
    }

问题来了 (它不会让我将其粘贴为平面图像。) My problem

嗯,我们一次删除一行,对吗?

好的,根据反馈,need/want标记如下:

 <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Header 2">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Column1") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 3">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Column2") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 4">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Column3") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                    <asp:Button ID="cmdDel" runat="server" Text="Delete" OnClick="cmdDel_Click"
                        OnClientClick="return confirm('Delete this row? (cannot undo)');"/>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
 </asp:GridView>
<br />

<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />

<asp:Button ID="cmdSave" runat="server" Text="Save Grid to Table" Style="margin-left:30px" OnClick="cmdSave_Click"/>

代码如下:

    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateTable();
            AddRow();
            LoadGrid();
            ViewState["MyTable"] = dt;
        }
        else
        {
            dt = (DataTable)ViewState["MyTable"];
        }
    }

    void CreateTable()
    {
        dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
        dt.Columns[0].AutoIncrementSeed = 1;
        dt.Columns[0].AutoIncrementStep = 1;
        dt.Columns[0].AutoIncrement = true;

        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    }

    void AddRow()
    {
        DataRow dr = dt.NewRow();
        dr["Column1"] = "";
        dr["Column2"] = "";
        dr["Column3"] = "";
        dt.Rows.Add(dr);
    }

    void LoadGrid()
    {
       // Gridview1.Columns.Clear();
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        SaveData();
        AddRow();
        LoadGrid();
    }

    void SaveData()
    {
        // move grid changes back to table
        foreach (GridViewRow gvRow in Gridview1.Rows)
        {
            //extract the TextBox values
            DataRow dr = dt.Rows[gvRow.RowIndex];
            dr["Column1"] = ((TextBox)gvRow.FindControl("TextBox1")).Text;
            dr["Column2"] = ((TextBox)gvRow.FindControl("TextBox2")).Text;
            dr["Column3"] = ((TextBox)gvRow.FindControl("TextBox3")).Text;
        }
        dt.AcceptChanges();
    }

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    protected void cmdDel_Click(object sender, EventArgs e)
    {
        Button cmdDel = (Button)sender;
        GridViewRow gvRow = (GridViewRow)cmdDel.Parent.Parent;

        dt.Rows[gvRow.RowIndex].Delete();
        dt.AcceptChanges();
        LoadGrid();
    }

当然,实际使用中的保存按钮将是“提交”并转到下一个网页,或者用户正在进行的任何任务。

我认为这只是为了学习,因为 table 只是坐在内存中几乎为零使用。我会假设 table 将来自数据库,否则此设置没有那么大用处。