如何使用gridview的内置删除命令按钮从Gridview中删除特定行 [英] how to delete a specific row from Gridview using builtin delete command button of gridview

查看:135
本文介绍了如何使用gridview的内置删除命令按钮从Gridview中删除特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从本地将文本框的数据添加到我的gridview中,而不是从数据库中添加数据.就像是推销员在推销产品的销售页面.现在我想从该网格视图中删除一行,但是删除时我总是异常.请帮助我.这是我的代码,我在其中获取数据并将其存储在gridview中.谢谢.

hi i am adding data from textboxes to my gridview locally not from database. its like a salepage where a salesman is saling a product. now i want to delete a row from that gridview but i always got exception while deleting. kindly help me with that. here is my code where i m getting the data and storing that in gridview. thanks.

DataTable dt1 = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gridVIEWData();
                GridView1.DataSource = dt1;
                GridView1.DataBind();
            }
    private void gridVIEWData()
        {
            
            dt1.Columns.Add("pName", typeof(string));
            dt1.Columns.Add("pCategory", typeof(string));
            dt1.Columns.Add("price", typeof(string));
            dt1.Columns.Add("pQuantity", typeof(string));
            dt1.Columns.Add("totalPrice", typeof(string));
            Session["dtInSession"] = dt1;   //Saving Datatable To Session
        }
    protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["dtInSession"] != null)
                dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 

            
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
                DataRow dr = dt1.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
                dt1.Rows.Add(dr);

                
                Session["dtInSession"] = dt1;     //Saving Datatable To Session 

                GridView1.DataSource = dt1;
                GridView1.DataBind();
            
            }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
           // string value = GridView1.DataKeys[e.RowIndex].Values["price"].ToString();
            //GridView1.DeleteRow(Convert.ToInt32(value));
            //GridView1.DeleteRow(GridView1.SelectedIndex);
            
               // dt1.Rows.RemoveAt(GridView1.SelectedIndex);
            Int32 index = GridView1.SelectedIndex;
            GridView1.DeleteRow(index);
        }

推荐答案

为什么不希望从数据库中获取数据以绑定到网格?如果正确使用分页和UpdatePanels,则数据和视图状态的占用空间应该非常低,以至于您不必担心要执行的操作.

另外,我更喜欢使用包含按钮(不是默认的删除按钮)的模板字段,将命令名称设置为"delete"或其他内容,并将数据记录的ID作为CommandArgument传递,并在后面处理OnRowCommand事件-end从数据库中删除内容,然后将网格重新绑定到数据库,以便此更改也可以在网格中反映出来.

您可以采用这种方法!
why do you not want to get the data from the database to bind to the grid? If you use paging and UpdatePanels properly, your data and viewstate footprint should be really low, so much so, that you won''t have to worry about doing what you are trying to do.

Plus, I prefer using a template field that contains a button (not the default delete button), set the command name to "delete" or whatever and pass in the ID of the data record as the CommandArgument and handle the OnRowCommand event in the back-end to delete stuff from the database and re-bind the grid to the database so that this change reflects back in the grid as well.

You can give this approach a go!


实际上我不想将该数据保存在数据库中.当用户在计算完整个账单后单击继续时,数据将被插入数据库中.因此,现在我必须在rumtime的gird视图中删除该行...您能帮忙我的代码吗?
actually i dont want to save that data in database. when the user will click on proceed after calculation whole bill than the data will be inserted in database. so for now i will have to delete the row from gird view on rumtime... can u help with my code.


这篇关于如何使用gridview的内置删除命令按钮从Gridview中删除特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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