Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
I want to delete the single row in gridview with using template control.I am using oracle database .when i click the delete i want to show the confirmation message box.
Thanks
Posted
Updated 18-Feb-12 0:17am
v2

1 solution

C#
<asp:gridview id="GridView1" runat="server" allowpaging="True" xmlns:asp="#unknown">
        AutoGenerateColumns="False" 
        DataSourceID="cmmentsdatasource" Width="992px" onrowcommand="rowcmd" 
        DataKeyNames='id' CellPadding="0" CssClass="style51" ForeColor="#333333">
        
        

        <rowstyle backcolor="#EFF3FB" font-bold="False" height="20px"></rowstyle>
        <columns>
        <asp:templatefield>
                 <itemtemplate>
                 <asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return confirm('Are You sure to delete this entry')" text="Delete" commandargument="<%#Eval("id")%>"></asp:linkbutton>
                 </itemtemplate>
                 
                 </asp:templatefield>
                
         <asp:boundfield datafield="id" headertext="ID" sortexpression="id" />
            <asp:boundfield datafield="senderid" headertext="SenderId">
                SortExpression="senderid" />
            <asp:boundfield datafield="sendername" headertext="SenderName">
                SortExpression="sendername" />
            <asp:boundfield datafield="comments" headertext="Comments">
                SortExpression="comments" />
            <asp:boundfield datafield="date" headertext="Date" sortexpression="date" />
        </asp:boundfield></asp:boundfield></asp:boundfield></columns>
       
        
        <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
       </footerstyle>
       
        

        <pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center">
       </pagerstyle>
       

        <selectedrowstyle backcolor="#D1DDF1" font-bold="True" forecolor="#333333">
        </selectedrowstyle>
       
        

        <headerstyle backcolor="#507CD1" font-bold="True" forecolor="White">
            Font-Size="Small" Height="20px">
        </headerstyle>
       
        
        <editrowstyle backcolor="#2461BF"></editrowstyle>
        

        <alternatingrowstyle backcolor="White"></alternatingrowstyle>
    </asp:gridview>

on row command event
C#
protected void rowcmd(object sender, GridViewCommandEventArgs e)
    {
      // create and open DB connection
        try
        {
           
            Int32 id = Convert.ToInt32(e.CommandArgument);
            string comm = "Delete from tblComments where id=@id";
            SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
            cmd.Parameters.AddWithValue("id", id);
            cmd.ExecuteNonQuery();
           GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

    }


O
 
Share this answer
 
Comments
Ramadurai Uthami 18-Feb-12 6:05am    
thank u sir but here when i click the cancel .the data is deleting . what i to do?
uspatel 18-Feb-12 6:10am    
<asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return confirm('Are You sure to delete this entry')" text="Delete" commandname="delete" commandargument="<%#Eval("id")%>">

and
use
protected void rowcmd(object sender, GridViewCommandEventArgs e)
{
// create and open DB connection
try
{
if(e.Commandname=="delete")
{
Int32 id = Convert.ToInt32(e.CommandArgument);
string comm = "Delete from tblComments where id=@id";
SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
cmd.Parameters.AddWithValue("id", id);
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
}
catch (Exception ex)
{
}

}

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