Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Developer,

How to delete the grid view row from ItemTempletefield.
In Itemtemplete have the linkbutton control.

Its grateful for me , if anybody share the code.

regards,
ravi sharma
Posted
Comments
Muralikrishna8811 12-Oct-11 7:33am    
you want to delete that whole item row

delete row in database and bind gridview

C#
protected void gvUserDetailsPend_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
 ds = // dataset binded to grid

 GridViewRow row = (GridViewRow)(((linkButton)e.CommandSource).NamingContainer);

DataRow[] dr = ds.Tables[0].Select(condition);
query = "DELETE FROM tablename WHERE condition";

belObj.DeleteRec(query);
ds.Tables[0].Rows.Remove(dr[0]);
gvUserDetailsPend.DataSource = ds;
gvUserDetailsPend.DataBind();

}

here DeleteRec() is method to delete from database
write commandname="Delete" for link button
 
Share this answer
 
v5
ASP.NET
<asp:datagrid id="" runat="server" autogeneratecolumns="False" oncancelcommand="OnCancel" width="100%" xmlns:asp="#unknown">
OnDeleteCommand="OnDelete">
<columns>

<asp:boundcolumn datafield="ID" headertext="ID" visible="false">                                        

<asp:templatecolumn>                                                
<itemtemplate>
<asp:imagebutton id="btnDelete" runat="server" imageurl="~/images/action_delete.png" tooltip="Delete" commandname="Delete" />
 </itemtemplate>      
 

</columns>




C#
protected void OnDelete(object s, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        try
        {
            int j;
            if (e.CommandName == "Delete")
            {
                j = e.Item.DataSetIndex;
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    ImageButton lbl = (ImageButton)e.Item.FindControl("btnDelete");
                    lbl.Attributes.Add("onclick", "return confirm (\"Really? Delete? \");");
                }
                int intItemID = Convert.ToInt32(e.Item.Cells[0].Text.Trim());
                IDataReader drItem = null;
                drItem = objDelete.DeletePOTFeature(intItemID, Convert.ToInt32(Session["USID"].ToString()), "N");
                BindGrid();
            }
            lblError.Text = "item deleted successfully";
            string alrtMsg = "";
            string strScript;
            alrtMsg = "Feature has deleted successfully";
            strScript = "<script language=JavaScript>";
            strScript += "alert(\"" + alrtMsg + "\");";
            strScript += "</script>";
            Page.RegisterStartupScript("clientScript", strScript);
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message.ToString();
        }
    }
 
Share this answer
 
v4

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