dhoni kholi

dhoni kholi

  • NA
  • 198
  • 39.5k

dynamically Add Linkbutton To Delete row in Gridview

Jan 5 2020 3:58 AM
How to Delete Row in datatable Dynamically created in Gridview 
 
This my code 
 
  1. <asp:GridView ID="GVShowdata" runat="server" RowStyle-Wrap="False"   
  2.                     BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" onrowdatabound="GVShowdata_RowDataBound"   
  3.                     onrowcommand="GVShowdata_RowCommand" onrowdeleting="GVShowdata_RowDeleting" CssClass="GridHeader" GridLines="Vertical">  
  4.                <AlternatingRowStyle BackColor="#DCDCDC" />  
  5.                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  6.                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
  7.                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  8.                <RowStyle Wrap="False" BackColor="#EEEEEE" ForeColor="Black"></RowStyle>  
  9.                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  10.                <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  11.                <SortedAscendingHeaderStyle BackColor="#0000A9" />  
  12.                <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  13.                <SortedDescendingHeaderStyle BackColor="#000065" />  
  14.            </asp:GridView>  
 
  1. protected void GVShowdata_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)  
  2.    {  
  3.        e.Row.Cells[0].Visible = false;  
  4.        e.Row.Cells[1].Visible = false;  
  5.        e.Row.Cells[2].Visible = false;  
  6.        e.Row.Cells[3].Visible = false;  
  7.        e.Row.Cells[4].Visible = false;  
  8.        e.Row.Cells[5].Visible = false;  
  9.        e.Row.Cells[6].Visible = false;  
  10.        e.Row.Cells[7].Visible = false;  
  11.        e.Row.Cells[8].Visible = false;  
  12.        e.Row.Cells[9].Visible = false;  
  13.        e.Row.Cells[16].Visible = false;  
  14.        e.Row.Cells[17].Visible = false;  
  15.        e.Row.Cells[18].Visible = false;  
  16.        e.Row.Cells[19].Visible = false;  
  17.        e.Row.Cells[20].Visible = false;  
  18.        e.Row.Cells[21].Visible = false;  
  19.        e.Row.Cells[22].Visible = false;  
  20.        e.Row.Cells[26].Visible = false;  
  21.        e.Row.Cells[27].Visible = false;  
  22.   
  23.        e.Row.Cells[10].Attributes["width"] = "150px";  
  24.        e.Row.Cells[11].Attributes["width"] = "75px";  
  25.        e.Row.Cells[12].Attributes["width"] = "75px";  
  26.        e.Row.Cells[13].Attributes["width"] = "75px";  
  27.        e.Row.Cells[14].Attributes["width"] = "50px";  
  28.        e.Row.Cells[15].Attributes["width"] = "50px";  
  29.        e.Row.Cells[23].Attributes["width"] = "75px";  
  30.        e.Row.Cells[24].Attributes["width"] = "75px";  
  31.        e.Row.Cells[25].Attributes["width"] = "75px";  
  32.        e.Row.Cells[28].Attributes["width"] = "150px";  
  33.   
  34.        if (e.Row.DataItem != null)  
  35.        {  
  36.            LinkButton lb = new LinkButton();  
  37.            lb.CommandArgument = e.Row.Cells[29].Text;  
  38.            lb.CommandName = "Delete";  
  39.            lb.Text = "Delete";  
  40.   
  41.            e.Row.Cells[29].Controls.Add((Control)lb);  
  42.        }  
  43.   
  44.    }  
  45.   
  46.    protected void GVShowdata_RowCommand(object sender, GridViewCommandEventArgs e)  
  47.    {  
  48.   
  49.        if (e.CommandName == "Delete")  
  50.        {  
  51.            GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);  
  52.            int RemoveAt = gvr.RowIndex;  
  53.            DataTable dt = new DataTable();  
  54.            dt = (DataTable)ViewState["Records"];  
  55.            dt.Rows.RemoveAt(RemoveAt);  
  56.            dt.AcceptChanges();  
  57.            ViewState["Records"] = dt;  
  58.        }  
  59.    }  
  60.   
  61.    protected void GVShowdata_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  62.    {  
  63.        GVShowdata.DataSource = ViewState["Records"];  
  64.        GVShowdata.DataBind();  
  65.    }  
 Output
 
 When I click Delete Link button 
 
That cell Only remove
Please Help me to How to remove Full row

Answers (6)