Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi frds,

I want to know how to delete a row in gridview. i will have a link button on each row
when user click on the link button the row should be removed.
Note:
i was using a
[Serializable]
list which was added to a view state from the view state i was binding it to a gridview.(i am not using database or datatable)how can i remove the data in gridview

thanks
parithi
Posted

 
Share this answer
 
You can see this article
GridView Delete, with Confirmation[^]
 
Share this answer
 
Hi
I did some code for your requirement check it once

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script language ="javascript" >
        function postrowtodel(id) {
            $.get("default12.aspx?action=delete", { name: id }, function (data) {
                location.reload();
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:datalist id="DataList1" runat="server" xmlns:asp="#unknown">
          <HeaderTemplate >
         <table width="100%"><tr><td>first col</td><td>second col</td></tr>
        </HeaderTemplate>
        <itemtemplate>
          <table><tbody><tr><td><a href="javascript:postrowtodel('<%# Container.DataItem %>')">Delete</a></td><td><![CDATA[<%# Container.DataItem %>]]></td></tr></tbody></table>
        </itemtemplate>
        <footertemplate>
         </footertemplate></table>
        
        </asp:datalist>
    </div>
    </form>
</body>
</html>


and code behind file contains following code

C#
//under PageLoad method
 if (!IsPostBack)
        {
            List<string> list; 
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString() == "delete")
                {
                    Response.Clear();
                    list = (List<string>)Session["DataSource"];
                    list.Remove(Request.QueryString["name"].ToString());
                    Response.Write("");
                    Response.End();
                }
            }
            else
            {
                if (Session["DataSource"] != null)
                {
                    list = (List<string>)Session["DataSource"];
                }
                else
                {

                    list = new List<string>();
                    list.Add("Bread");
                    list.Add("Cheeze");
                    list.Add("Wine");
                    list.Add("Beer");
                    list.Add("Waffles");
                    Session["DataSource"] = list;
                   
                }
                DataList1.DataSource = list;
                DataList1.DataBind();
            }
        }
</string></string></string></string>


I hope you understood what I did

All the Best
 
Share this answer
 
you have select that row and
check
wheather

C#
if (this.dataGridView1.SelectedRows.Count > 0)
               {
                   fire query to delete row
                   
               }
               else
               {
                   MessageBox.Show("Please Select Row First To Delete", "Library Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
 
Share this answer
 
Hi try this, hope this may help you


ASP.NET
<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        alert("hii");
        var person = "";
        $("#button1").click(function () {
            alert("in button click event");
            $("#<%=TestGridView.ClientID  %> tr").each(function () {
                if (!this.rowIndex) return;
                var age = $(this).find("td:last").html();
                person += age + "<br />";
            });
            $("#personage").html(person)
        });
    });
    function test() {
        alert("hi11");
        var person = "";
// Here I am iterating through the grid view get the index here then your requirement will be achieved
        $("#<%=TestGridView.ClientID  %> tr").each(function () {
            // alert("in button click event");
            if (!this.rowIndex) return;
            $(this).find("td:last").remove();
            $(this).find("td:first").remove();
            //person += age + "<br />";
        });
        //$("#personage").html(person)
    }
</script>
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
 <asp:gridview id="TestGridView" runat="server"></asp:gridview>
    <input type="button" id="button1" value="submit" runat="server" onclick="javascript:test();" />

</asp:content>
 
Share this answer
 
v2
hello friend
u can refer this site for..................http://www.dotnetfunda.com/articles/article29.aspx[^]
 
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