Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.57/5 (5 votes)
See more:
hi
i had one requirement it is like:

i have a grid which gets bind the data from database through datasource now my requirement is if i click on delete button which is through showdeletebutton=true property in the grid only the record should be deleted from grid and not in databse.
once i click the complete button the record should be deleted from database also...

please help
Posted
Comments
Gitanjali Singh 27-Dec-13 4:47am    
you can set flag for that record in database on click of delete button,and populate records in grid view accordingly.
Sampath Lokuge 27-Dec-13 4:59am    
You have to use client side trick for do that. Jquery is suitable for that.
King Fisher 27-Dec-13 5:17am    
you can maintain the status column for all as '1' if you click delete button the make it as '0' and just select * from tbl_sample where status=1 ,
spanner21 27-Dec-13 5:36am    
i cant touch the database,,,can i do it via coding only please?
King Fisher 27-Dec-13 6:28am    
else u can visible = false the Row

Try this:
Assign:
static int f;
static string chain;

In the delete button click event of gridview use this code snippet:
using (GridViewRow row = (GridViewRow)((ImageButton)sender).Parent.Parent)
       {

           Label lblid1 = (Label)row.FindControl("lbl_id");
           int id1 = Convert.ToInt16(lblid1.Text);

           if (f == 0)
           {
               chain += id1.ToString();
           }
           else if (f != 0)
           {
               chain += ',' + id1.ToString();
           }

           f++;

           ImageButton btn = (ImageButton)sender;

           GridView1.Rows[row.RowIndex].Visible = false;

       }

and in complete button click event
C#
string[] text = chain.Split(',');
 foreach(string word in text)
 {
//delete query here where id=word
//Console.WriteLine(word);  
 }


chain = "";


or try it:

1.Get the ID from selected Row of gridview on delete button click

2.Remove the particular record from Datasource(DataTable or DataSet) and store the Selected ID in a variable

3.Then again bind it with gridview

4.on complete Button click get the id and delete it.
 
Share this answer
 
v3
Comments
Tom Marvolo Riddle 27-Dec-13 6:54am    
Try it and let me know
spanner21 27-Dec-13 6:57am    
Hi Jas,

I tried like


OnRowDeleting event i wrote:

dbTable.Rows[e.RowIndex].Delete();
Gridview.DataSource =dbTable;
GridView.DataBind();
Tom Marvolo Riddle 27-Dec-13 7:10am    
Hello spanner ,it's better to get the ID rather than index from gridview .Then it is easy to match with datatable(Datasource).if you use row index it may not match with datasource record and it will ruin all.
please see this link:
http://stackoverflow.com/a/15249188

you have to do similarly and feel free to ask.
Tom Marvolo Riddle 27-Dec-13 7:15am    
Or you have to do it in Client side (Javascript or Jquery).
Sorry buddy but i don't know both these things.But i guess it will easy in clientside
Tom Marvolo Riddle 27-Dec-13 8:12am    
Hi spanner.,try this link:

http://www.codeproject.com/Answers/356585/gridview-row-deletion-without-database#answer2

hope it will work.
Try using the
RemoveAt()
method for the gridview rows to have a soft delete.
This wont delete from the DB but only from the gridview collection.
 
Share this answer
 
First you need to and a column 'status' in your DB Table and in the Column properties of 'status' Set the Default Value or Binding property to 1 , so for every new entry 'Status' column is automatically set to 1. Then load your datagrid view with rows having 'status=1' like

select * from [Table_name] where status=1


Then in the row deleting event of grid view you should update the status of record u need to delete to 0

string str = GridView1.DataKeys[e.RowIndex].Values["id"].ToString(); 
        SqlDataAdapter da = new SqlDataAdapter("UPDATE [Table_name] set status=0 WHERE id = '" + str + "' ", connStr);



then again call the function to load grid,
 
Share this answer
 
Comments
Tom Marvolo Riddle 28-Dec-13 1:09am    
Hi praveen,i agree with your answer but OP wants to do without accessing the Database.
please see his comments
Praveen_P 28-Dec-13 1:43am    
k, sorry i forgot to check his comments
Tom Marvolo Riddle 28-Dec-13 2:34am    
hey,it's not a big deal :)
XML
In aspx page add , one template field like above.....

<Columns>
 <asp:TemplateField HeaderText="Hide"> <ItemTemplate> <asp:LinkButton ID="lnkHide" runat="server" Text="Hide" OnClick="LnkHide_Click" /> </ItemTemplate> </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" />

            //Other columns you have added as Boundfield like above

</Columns>


Than in aspx.cs page , write this function:


protected void LnkHide_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
clickedRow.Visible = false;
}





You just have to hide your selected row..
C#
protected void XYZ_SelectedIndexChanged(object sender, EventArgs e)

   {

       XYZ.Rows[XYZ.SelectedIndex].Visible = false;

   }
 
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