Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Sir !
What is the syntax of delete operation in asp.net website. I want to remove the records from the gridview.
Posted
Updated 24-Sep-12 4:06am
v2
Comments
joshrduncan2012 24-Sep-12 10:08am    
Have you tried googling for the answer to see if you can find it yourself?

For deleting record u can try like that :
C#
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Find the unique column of row 
        Label lblId = (Label)gvDetails.Rows[e.RowIndex].FindControl("lblSno");
        DataTable dt = new DataTable();
        dt = Session["Rol"] as DataTable;
      // delete the record from data table 
        for (int i = 0; i < dt.Rows.Count; i++)
        {
             //  
            if (dt.Rows[i][0].ToString() == lblId.Text)
            {
               // write the code for deleting row here .
            }
        }

        dt.AcceptChanges();
        Session["Rol"] = dt;
        fnBindGrid();// function for binding grid otherwise  u can  bind here .
    }
 
Share this answer
 
v2
HTML
<asp:gridview id="grid_symptom" cssclass="grid" autogeneratecolumns="False" runat="server" xmlns:asp="#unknown">
                                                    DataKeyNames="symptom_id" 
                                                    OnRowDeleting="grid__symptom_RowDeleting" OnRowEditing="grid_symptom_RowEditing"
                                                    OnRowCancelingEdit="grid_symptom_RowCancelingEdit" OnRowUpdating="grid_symptom_RowUpdating"
                                                    PageSize="3" AllowPaging="True" 
                                                    OnPageIndexChanging="grid_symptom_PageIndexChanging">
                                                    <alternatingrowstyle backcolor="Beige" />
                                                    <pagerstyle cssclass="paging" horizontalalign="Center" />
                                                    <columns>
                                                        <asp:templatefield headertext="Symptoms">
                                                            <edititemtemplate>
                                                                <asp:textbox id="txt_edit_symptom" runat="server" text="<%#Eval ("symptom_name") %>"></asp:textbox>
                                                            </edititemtemplate>
                                                            <itemtemplate>
                                                                <asp:label id="lbl_item_symptom" runat="server" text="<%#Eval ("symptom_name") %>"></asp:label>
                                                            </itemtemplate>
                                                        </asp:templatefield>
                                                        <asp:templatefield headertext="Action">
                                                            <edititemtemplate>
                                                                <asp:imagebutton id="Imgbtn_symptom_Update" commandname="Update" runat="server" imageurl="~/Admin/images/update1.jpg">
                                                                    ToolTip="Update" />
                                                                <asp:imagebutton id="Imgbtn_symptom_Cancel" runat="server" commandname="Cancel" imageurl="~/Admin/images/cancel1.jpg">
                                                                    ToolTip="Cancel" />
                                                            </asp:imagebutton></asp:imagebutton></edititemtemplate>
                                                            <itemtemplate>
                                                                <asp:imagebutton id="Imgbtn_symptom_edit" commandname="Edit" tooltip="Edit" imageurl="~/Admin/images/edit2.png">
                                                                    runat="server" />    
                                                                <asp:imagebutton id="Imgbtn_symptom_delete" commandname="Delete" tooltip="Delete" imageurl="~/Admin/images/delete2.png">
                                                                    OnClientClick="return confirm('Are you sure,Do you want to delete this Symptoms?');"
                                                                    runat="server" />
                                                            </asp:imagebutton></asp:imagebutton></itemtemplate>
                                                        </asp:templatefield>
                                                    </columns>
                                                    <headerstyle cssclass="gridHeader" />
                                                    <footerstyle backcolor="Black" />
                                                    <rowstyle cssclass="gridRow" />
                                                    <pagersettings firstpagetext="<br" mode="hold" />

C#
protected void grid__symptom_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
       MessageHide();
      int i = 0;
      try
      {
          int symptomid = int.Parse(grid_symptom.DataKeys[e.RowIndex].Values[0].ToString());
          bo.Symptom_id= symptomid;
          i = bl.Deletesymptom(bo);
      }
      catch (Exception ex)
      {
          ex.ToString();
      }
      if (i > 0)
      {
          divDeleteSuc.Visible = true;
      }
      else
      {
          divDeleteUsuc.Visible = true;
      }
      bindgridsymptom();
  }
  protected void grid_symptom_RowEditing(object sender,GridViewEditEventArgs e)
  {
      grid_symptom.EditIndex = e.NewEditIndex;
      MessageHide();
      bindgridsymptom();
  }
  protected void grid_symptom_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
      grid_symptom.EditIndex = -1;
      MessageHide();
      bindgridsymptom();
  }
  protected void grid_symptom_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
      MessageHide();
      int i = 0;
      try
      {
          TextBox txtsymptom = (TextBox)grid_symptom.Rows[e.RowIndex].FindControl("txt_edit_symptom");
          int symptomid= int.Parse(grid_symptom.DataKeys[e.RowIndex].Values[0].ToString());
          bo.Symptom_id = symptomid;
          bo.Symptom_nm = txtsymptom.Text;
          i = bl.Updatesymptom(bo);
      }
      catch (Exception ex)
      {
          ex.ToString();
      }
      if (i > 0)
      {
          divGridUpdateSuc.Visible = true;
      }
      else
      {
          divGridUpdateUSuc.Visible = true;
      }
      grid_symptom.EditIndex = -1;
      bindgridsymptom();
  }
  protected void grid_symptom_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
      grid_symptom.PageIndex = e.NewPageIndex;
      MessageHide();
      bindgridsymptom();
  }
 
Share this answer
 
v2
its depend on condition,after click on delete button u r hiting database or after click on save button so tell me what r doing ?
 
Share this answer
 
Comments
tusharkaushik 25-Sep-12 15:01pm    
after clicking on the button

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