Click here to Skip to main content
15,503,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
        }
Posted

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!
 
Share this answer
 
v2
Comments
Nelek 15-Oct-12 17:45pm    
OP is telling you this (moved here from a no-solution):
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.
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.
 
Share this answer
 
Comments
Nelek 15-Oct-12 17:46pm    
If you need to answer someone giving you a solution, please don't add another solution. You can use the "have a question or comment" (as I am doing right now with you). This way the user will get a notification and the board will stay clean of not relevant things.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900