Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi How To Delete Records From GridView but Not From DataBase.?can Any one help..i tried one code but its not deleting from gridview..
here is my Code..
C#
DataTable dt =new  DataTable();
        if (grdCart.Rows.Count <=1)
        {
            e.Cancel = true;
            Response.Write("You Must Keep One record");
        }
        else
        {
            if (grdCart.SelectedIndex >= 0)
            {
                dt.Rows.RemoveAt(grdCart.SelectedIndex);
                e.Cancel = false;
                grdCart.DataSource =dt;
                grdCart.DataBind();
                grdCart.Visible = true;
            }
        }
Posted
Updated 30-Dec-11 22:18pm
v2
Comments
ythisbug 31-Dec-11 6:04am    
hi any other solution??

1. u can use a dataset and datatable to get the result.

2, u can have a "flag" field in the table and set the flag when u click the buttons.

or , try in the following way:
bind the data from database to a datatable like "dbTable". Use dbTable to bind the gridview.

on gridview_rowDeleting ...write following code...

C#
dbTable.Rows[e.RowIndex].Delete();

Gridview.DataSource =dbTable;

GridView.DataBind();
 
Share this answer
 
v2
Comments
ythisbug 31-Dec-11 4:37am    
There is no row at position 0.
here am getting this error..
Mehdi Gholam 31-Dec-11 5:19am    
Please do not use text/chat speak when answering questions.
ythisbug 31-Dec-11 6:04am    
ok
Mehdi Gholam 31-Dec-11 6:12am    
I'm sorry I was referring to pratibhakamaji.
GridView.DeleteRow Method [^] which is used to deletes the record at the specified index from the data source.
Have a look there[^]
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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