如何从此数据表中删除行? [英] How to delete a row from this Datatable?

查看:265
本文介绍了如何从此数据表中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码,当我单击按钮时,我想删除数据表的最后一行并将该表保存在会话中.我怎样才能做到这一点??这是我的代码.

i have this code and when i click a button i wanna delete the last row of the data table and save that table in a session. how can i do that?? this is my code..

public partial class Myreservations : System.Web.UI.Page
{


    DBAccess isusrobj = new DBAccess();


    protected void Page_Load(object sender, EventArgs e)
    {
        String email = isusrobj.setSessionVar();

        if (email != (String)Session["email"]) 
        {

            HyperLink choosemore = new HyperLink();
            ImageButton cancelDVD = new ImageButton();
            ImageButton BookDVDs = new ImageButton();

            choosemore.ImageUrl = "~/Images/more.gif";
            choosemore.NavigateUrl = "Movies.aspx";

            cancelDVD.ImageUrl = "~/Images/cancel.gif";
            cancelDVD.OnClientClick = cancelDVDs(); //this is where i wanna delete the rows

            BookDVDs.ImageUrl = "~/Images/bookdvds.gif";
            BookDVDs.OnClientClick = BookDVDsinDB();

            Panel1.Controls.Add(choosemore);
            Panel1.Controls.Add(cancelDVD);
            Panel1.Controls.Add(BookDVDs);


            String Mname = Request.QueryString["Vn"];
            String Myear = Request.QueryString["Vy"];
            String Mstars = Request.QueryString["Vs"];


            DataTable bookingtable = (DataTable)Session["table"];
            DataRow row = bookingtable.NewRow();
            row["Movie Name"] = Mname;
            row["Movie Year"] = Myear;
            row["Starring"] = Mstars;

            if (bookingtable.Rows.Count == 0)
            {

                bookingtable.Rows.Add(row);
                bookingtable.AcceptChanges();
                Reservations.DataSource = bookingtable;
                Reservations.DataBind();
                Session.Add("table", bookingtable);



            }
            else
            {
                bookingtable = (DataTable)Session["table"];
                bookingtable.Rows.Add(row);
                bookingtable.AcceptChanges();
                Session.Add("table", bookingtable);
                Reservations.DataSource = bookingtable;
                Reservations.DataBind();
            }



        }
        else
        {
            loginLabel.Text = "You have not logged in.Please log in to book the DVD!";
        }
    }

    private string BookDVDsinDB()
    {
  
        throw new NotImplementedException();
    }

    private string cancelDVDs()
    {

        throw new NotImplementedException();
    }

推荐答案

bookingtable = (DataTable)Session["table"];
if(bookingtable.Rows.Count > 0) bookingtable.Rows(bookingtable.Rows.Count - 1).Delete();
bookingtable.AcceptChanges();
Session.Add("table", bookingtable);
Reservations.DataSource = bookingtable;
Reservations.DataBind();


要删除DataTable的最后一行,请使用此方法. [替代解决方案]
To delete last row of DataTable use this..
[Alternate Solution]
bookingtable.Rows.RemoveAt(bookingtable .Rows.Count-1]);


这篇关于如何从此数据表中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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