You must Sign In to post a response.
  • Category: ASP.NET

    Delete row in asp.net gridview

    Hiello, Developers,
    i want to delete a row in gridview this code is working but i did not got a exactly output. So any correction meavs please tell me friends.
    my cs code
    protected void grdupdatecalender_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    Label lblid = (Label)grdupdatecalender.Rows[e.RowIndex].FindControl("lblEditid");
    int i = objadmin.Deletetextbox(Convert.ToInt32(lblid.Text.Trim()));
    if (i > 0)
    {
    lblmsg.Visible = true;
    lblmsg.Text = "Record Deleted Successfully";
    }
    grdupdatecalender.EditIndex = -1;
    bindgrid();

    }

    my class file code
    public int Deletetextbox(long lblId)
    {
    string strSql = "update tbl_Petprofile set Status='D' where Id=@lblId";
    // string strSql = "Delete from tbl_Petprofile where lblId=@lblId";
    OleDbConnection conn = new OleDbConnection(conStr);
    if (conn.State == ConnectionState.Open)
    conn.Close();
    conn.Open();
    OleDbCommand cmd = new OleDbCommand(strSql, conn);
    OleDbParameter param = new OleDbParameter("@lblId", lblId);
    cmd.Parameters.Add(param);

    try
    {
    return cmd.ExecuteNonQuery();

    }
    catch
    {
    throw;
    }
    finally
    {

    conn.Dispose();
    conn.Close();

    }
    }
    #endregion

    my aspx code for gridview

    <asp:GridView ID="grdupdatecalender" OnRowDataBound="grdupdatecalender_RowDataBound" Height="40%" Width="100%"
    CellPadding="4" OnPageIndexChanging="grdupdatecalender_PageIndexChanging" OnRowCommand="grdupdatecalender_RowCommand"
    OnRowCancelingEdit="grdupdatecalender_RowCancelingEdit" OnRowDeleting="grdupdatecalender_RowDeleting" OnRowEditing="grdupdatecalender_RowEditing"
    OnRowUpdating="grdupdatecalender_RowUpdating" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
    <Columns>
    <asp:TemplateField HeaderText="Id" Visible="false">
    <ItemTemplate>
    <asp:Label id="lblEditid" Visible="false" runat="server" Text='<%#Eval("id") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    Thanks with
    Paul.S
  • #762088
    Hi Paul,

    Your code seems to be Ok, Are you getting any error.? While delete the record, as per your post it is working fine. If it is working fine then obviously it should give result perfectly. If you get any errors please share to us, otherwise the best solution is put break point and check line by line that is the best way to resolve errors.

    Hope you understood..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762092
    Hi

    Paul

    You can trace step by in your code. Can you share your issue what error did you facing ?

    then

    see your code this line you have assign long data type variable this method name this Deletetextbox but you passing int datatype why we go to long? mostly id comes below 5 digit so why you go to long can you change long to int data type.


    Then you can use parameter or Query means in your code you must mention command type also add this code. This is missing your code.But Parameters added so

    If you Handled Stored Procedure means try this code


    cmd.CommandType = CommandType.StoredProcedure;


    If you Handled Direct Query means try this code


    cmd.CommandType=CommandType.Text




    int i = objadmin.Deletetextbox(Convert.ToInt32(lblid.Text.Trim()));

    public int Deletetextbox(long lblId)
    {

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762094
    Hi

    As you have written conn.Close() method in finally block it will execute compulsory irrespective of exception and any other problems. Why again you are checking like
    if (conn.State == ConnectionState.Open)// no need
    conn.Close();// no need
    conn.Open(); // just open connection

    Regards

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762099
    Hello Suresh,

    <asp:GridView DataKeyNames="CategoryID" ID="GridView1"
    runat="server" AutoGenerateColumns="False"
    OnRowCommand="GridView1_RowCommand"
    OnRowDataBound="GridView1_RowDataBound"
    OnRowDeleted="GridView1_RowDeleted"
    OnRowDeleting="GridView1_RowDeleting">
    </asp:GridView>

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
    GridView1.DeleteRow(categoryID);
    }

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762108
    Hi

    You can go through this code

    Client Side


    <asp:GridView DataKeyNames="ID" ID="GridView2"
    runat="server" AutoGenerateColumns="False" selectedindex="0"
    OnRowDeleting="GridView2_RowDeleting" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" />
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:BoundField DataField="EmpNo" HeaderText="EmpNo" />
    <asp:BoundField DataField="Amount" HeaderText="Amount" />
    <asp:CommandField ShowDeleteButton="True" />
    </Columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
    </asp:GridView>


    Server Side

    Bind Records



    DataTable dt = new DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    dt.Columns.Add("EmpNo");
    dt.Columns.Add("Amount");
    DataRow dr;
    dr = dt.NewRow();
    dr[0] = 1;
    dr[1]="AAA";
    dr[2] = 1234;
    dr[3] = 7500.25;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 8;
    dr[1] = "AAA";
    dr[2] = 1234;
    dr[3] = 7500.25;
    dt.Rows.Add(dr);


    dr = dt.NewRow();
    dr[0] = 11;
    dr[1] = "AAA";
    dr[2] = 1234;
    dr[3] = 7500.25;
    dt.Rows.Add(dr);

    GridView2.DataSource = dt;
    GridView2.DataBind();
    ViewState["dt"]=dt;


    Delete the Record . Gridview RowDeleting Event this


    int index = Convert.ToInt32(e.RowIndex);
    DataTable dt = ViewState["dt"] as DataTable;
    dt.Rows[index].Delete();
    GridView2.DataSource = dt;
    GridView2.DataBind();

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762109
    Hi
    Nirav

    Your code this line i tried not working again and again calling GridView1_RowDeleting event May i know how to use Gridview public virtual void DeleteRow(int rowIndex) ?;

    GridView1.DeleteRow(categoryID);

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762111
    Hii Kumar,

    Syntax :

    GridView.DeleteRow(int RowIndex);

    So you can used
    GridView1.DeleteRow(1);

    I hope you understand.

    Refer this link of MSDN Code Library :

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.deleterow(v=vs.110).aspx

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762117
    Hi
    Nirav

    I hearing new in this command so i am asking to you

    GridView1.DeleteRow

    after execute this line I face the issue i have attached image

    GridView1.DeleteRow

    Can you check this snapshot given below

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment


Sign In to post your comments