Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
How to delete row in a gridview and database also using c# asp.net code?

I am not using wizard or SqlDataSource,
I want to delete row, my StoredProcedure.

Image Button not firing of gridview using OnDeleting Command.

How can we delete row from database and Gridview, Please Help Me.
My Following Code Is:-

XML
<asp:GridView ID="GvMyCart" runat="server" CssClass="dataTable"
                AutoGenerateColumns="False" RowStyle-CssClass="odd_row"
                AlternatingRowStyle-CssClass="even_row"
                onrowdatabound="GvMyCart_RowDataBound" ShowFooter="True"
            OnRowDeleting="GvMyCart_RowDeleting" onrowcommand="GvMyCart_RowCommand">
            <AlternatingRowStyle CssClass="even_row" />
        <Columns>

            <asp:TemplateField HeaderStyle-CssClass="dataTableHeader">
            <ItemTemplate>

            <asp:ImageButton runat="server" ID="IgmBtnDeleteCard"
                    ImageUrl="~/images/remove-icon.png" CommandName="Delete" CommandArgument='<%#Eval("CardID")%>'>
            </asp:ImageButton>

            </ItemTemplate>
                <HeaderStyle CssClass="dataTableHeader" />
            </asp:TemplateField>
        <asp:TemplateField HeaderStyle-CssClass="dataTableHeader" HeaderText="S.No.">
        <ItemTemplate>
            <%# ((GridViewRow)Container).RowIndex + 1%>
        </ItemTemplate>
            <HeaderStyle CssClass="dataTableHeader" />
        </asp:TemplateField>


C#
protected void GvMyCart_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            var id = Int32.Parse(e.CommandArgument.ToString());
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd1 = new SqlCommand("procdeleteCard", con);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@CardID", SqlDbType.Int).Value = Convert.ToInt32(id);
            cmd1.ExecuteNonQuery();
            con.Close();
            BindGrid();
            //Delete Code using the id

            //Rebind GridView
        }
    }


Please help me.

Thanks in Advance.
Ankit Agarwal
Web Developer
Posted
Updated 28-Dec-13 1:48am
v2
Comments
Sampath Lokuge 28-Dec-13 8:00am    
Is your 'GvMyCart_RowCommand' event firing,when you click delete button ?

C#
void GvMyCart_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
     var id = Int32.Parse(e.CommandArgument);
     GVDetail.DeleteRow(id);
//bind your Grid ,it must
 }
}
 
Share this answer
 
v2
I can see one problem for which RowCommand won't fire. Name of the event is case sensitive.

You have...
HTML
onrowcommand="GvMyCart_RowCommand"

But it should be..
HTML
OnRowCommand="GvMyCart_RowCommand"
 
Share this answer
 
in source view
C#
<asp:gridview id="GvMyCart" runat="server" cssclass="dataTable" xmlns:asp="#unknown">
                AutoGenerateColumns="False" ---------------
                --------------------------
            OnRowDeleting="GvMyCart_RowDeleting" >
<asp:imagebutton runat="server" id="IgmBtnDeleteCard">
                    ImageUrl="~/images/remove-icon.png" CommandName="Delete" >
            </asp:imagebutton>
</asp:gridview>

and write your logic in GvMyCart_RowDeleting event
 
Share this answer
 
How to delete multiple row in database using gridview in asp.net?

<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
DataKeyNames="Emp_ID" style="text-align: center">
<columns> <asp:boundfield datafield="Emp_ID" headertext="Emp_ID" insertvisible="False" readonly="True">
SortExpression="Emp_ID" />
<asp:boundfield datafield="FName" headertext="FName" sortexpression="FName">
<asp:boundfield datafield="LName" headertext="LName" sortexpression="LName">
<asp:boundfield datafield="Address" headertext="Address" sortexpression="Address">
<asp:boundfield datafield="E-Mail" headertext="E-Mail" sortexpression="E-Mail">
<asp:templatefield headertext="Action">
<itemtemplate>
<asp:checkbox id="chkSelect" runat="server" oncheckedchanged="chkStatus_OnCheckedChanged" value="Emp_ID" autopostback="true">





Quote:
protected void btnupdt_Click(object sender, EventArgs e)
{

string ids = "";
foreach (GridViewRow di in GridView1.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("chkSelect");

if (chkBx != null && chkBx.Checked)
{
ids = ids + GridView1.DataKeys[di.RowIndex].Value.ToString() + " ";
}
}

if (!string.IsNullOrEmpty(ids))
{
string str = "update Emp_Mgt set FName='" + txtfrstnm.Text + "',LName='" + txtlstnm.Text + "',Address='" + txtadrs.Text + "',[E-Mail]='" + txtemil.Text + "' where Emp_Id=" + ids;
SqlCommand cmd = new SqlCommand(str, con);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

loadgrid();

lblchk.Text = "("+ids+") Row Is Update....!";
}

protected void btndlt_Click(object sender, EventArgs e)
{
string ids = "";
foreach (GridViewRow di in GridView1.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("chkSelect");

if (chkBx != null && chkBx.Checked)
{
ids = ids + GridView1.DataKeys[di.RowIndex].Value.ToString() + ",";
}
}

if (!string.IsNullOrEmpty(ids))
{
SqlCommand cmd = new SqlCommand("delete from Emp_Mgt where Emp_ID IN (" + ids.Trim(',') + ")", con);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

loadgrid();

lblchk.Text = "(" + ids.Trim(',') + ") Rows Are Delete....!";
}

protected void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{

SqlDataReader sdr;

string ids = "";
foreach (GridViewRow di in GridView1.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("chkSelect");

if (chkBx != null && chkBx.Checked)
{
ids = ids + GridView1.DataKeys[di.RowIndex].Value.ToString() ;
}
}


if (!string.IsNullOrEmpty(ids))
{
string str = "select FName,LName,Address,[E-Mail] from [emp_mgt] where Emp_Id=" + ids;
SqlCommand cmd = new SqlCommand(str, con);

con.Open();

sdr = cmd.ExecuteReader();

while (sdr.Read())
{
txtfrstnm.Text = sdr["FName"].ToString();
txtlstnm.Text = sdr["LName"].ToString();
txtadrs.Text = sdr["Address"].ToString();
txtemil.Text = sdr["E-Mail"].ToString();
}

con.Close();
}

}
 
Share this answer
 

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