从网格视图动态删除行 [英] Dynamically removing rows from a grid view

查看:94
本文介绍了从网格视图动态删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,以通过单击可用按钮将行动态添加到网格视图. ADD按钮将向网格视图添加新行,而Delete按钮将删除一行.
我想将删除限制为已插入的新行,但仅限于添加"按钮.当前,该代码允许用户删除填充网格视图的第一条记录,我想防止这种情况.
有人可以给我个主意吗?

最初加载的第一个表单:我将文本添加到文本框中,然后单击Add

第一张图片

单击添加后将加载的第二种形式:

第二张图片

如果我在红色突出显示的字段上单击删除",则我不想删除它,因为这是我的第一笔记录.在这种情况下,我想删除以删除以后的记录.

这是我的代码:

I am writing code to dynamically add rows to grid view by clicking on the buttons available. The ADD button will add new rows to grid view and the Delete button will delete a row.
I would like to restrict deletion to new rows that were inserted but as by the Add button. Currently, the code allows users to delete the first record populating the grid view and I would like to prevent this.
Can any one give me an idea?

First form which was initially loaded: I am adding text to text box and later I will click on Add

First Image

Second form which will be loaded after clicking ADD:

Second Image

If I click Delete on the red highlighted field, I don''t want to delete it, as it is my first record. In this case, I would like delete to remove the later record.

Here is my code:

protected void btndelete_click(object sender, EventArgs e)
    {
        Button lb = (Button)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        int rowID = gvRow.RowIndex + 1;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 1)
            {
                if (gvRow.RowIndex < dt.Rows.Count - 1)
                {
                    //Remove the Selected Row data
                    dt.Rows.Remove(dt.Rows[rowID]);
                }
            }
            //Store the current data in ViewState for future reference
            ViewState["CurrentTable"] = dt;
            //Re bind the GridView for the updated data
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

   }



感谢您的帮助.



Any help is appreciated.

推荐答案

我首先想到的是;

您可能有一个名为标志",状态"或类似名称的列.使该列可见性为假.在第一次加载gridview时,gridview中的foreach行将这个"flag"值设置为"false".因此,当用户添加新列时,将标志列"值分配为true.因此,在删除数据时,请检查此列是否为true或false.这可能不是最好的答案,但可能会暂时解决您的问题.
Well first coming to my mind is;

You might have a column named "flag", "status" or something like that. Make that column visibilty false. At first load of the gridview foreach row in the gridview make this "flag" values as "false". So when the user adds a new column assign the "flag column" value to true. So while deleting the data check this column if it is true or false. This might not be the best answer but it might solve your problem temporary.


是的,首先要解决.首先暂时解决问题.还有一点需要注意,不是使可见性为假,而是从datable删除行并调用Grid填充函数....
Yeah, first things first. Solve the problems temporarily first. One more point which is coming in mind, instead of making the visibility false, delete the row from datable and Call Grid Populating Function....


这篇关于从网格视图动态删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆