locked
How To Remove Selected Data From Gridview RRS feed

Answers

  • User-369506445 posted

    hi

    I created a sample for you with 2 drop-down , the one fill items in gridview and another one remove item from gridview

    In Code Behind

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Session["GridDataTable"] = null;
                    FillDropDown();
                    FillDropDown2();
                }
            }
            protected void ddl_OnSelectedIndexChanged(object sender, EventArgs e)
            {
                string text = ddl.SelectedItem.Text;
                var dt = getSearchItems(text);
                if (Session["GridDataTable"] == null)
                {
                    Session["GridDataTable"] = dt;
                    grd.DataSource = dt;
                    grd.DataBind();
                    return;
                }
    
                DataTable lastDataTable = (DataTable)Session["GridDataTable"];
                lastDataTable.Merge(dt);
    
                DataTable distinctTable = lastDataTable.DefaultView.ToTable( /*distinct*/ true);
                Session["GridDataTable"] = distinctTable;
                grd.DataSource = Session["GridDataTable"];
                grd.DataBind();
    
            }
            protected void ddl2_OnSelectedIndexChanged(object sender, EventArgs e)
            { 
                string text = ddl2.SelectedItem.Text;
                var dt = getSearchItems(text);
                if (Session["GridDataTable"] == null)
                {
                    return;
                }
    
                DataTable lastDataTable = (DataTable)Session["GridDataTable"];            
                var removeTable = lastDataTable.AsEnumerable().Except(dt.AsEnumerable(), DataRowComparer.Default) ;
    
                if (removeTable.Any())
                    Session["GridDataTable"] = removeTable.CopyToDataTable();
                else
                    Session["GridDataTable"] = null;
    
                grd.DataSource = null;          
                grd.DataSource = Session["GridDataTable"];
                grd.DataBind();
    
            }
    
            void FillDropDown()
            {
                const string query = "select distinct [ItemName] from yourTable";
                using (var con = new SqlConnection(ConnectionString))
                {
                    con.Open();
                    using (var com = new SqlCommand(query, con))
                    {
                        SqlDataReader sdrselect = com.ExecuteReader();
    
                        ddl.DataTextField = "ItemName";
                        ddl.DataValueField = "ItemName";
                        ddl.DataSource = sdrselect;
                        ddl.DataBind();
                         
                    }
                }
            }
            void FillDropDown2()
            {
                const string query = "select distinct [ItemName] from yourTable";
                using (var con = new SqlConnection(ConnectionString))
                {
                    con.Open();
                    using (var com = new SqlCommand(query, con))
                    {
                        SqlDataReader sdrselect = com.ExecuteReader();
                         
                        ddl2.DataTextField = "ItemName";
                        ddl2.DataValueField = "ItemName";
                        ddl2.DataSource = sdrselect;                     
                        ddl2.DataBind();
    
                    }
                }
            }
    
    
            DataTable getSearchItems(string param)
            {
    
                string query = "select * from yourTable where ItemName='" + param + "'";
                using (var con = new SqlConnection(ConnectionString))
                {
                    con.Open();
                    using (var com = new SqlCommand(query, con))
                    {
                        using (SqlDataReader dr = com.ExecuteReader())
                        {
                            var tb = new DataTable();
                            tb.Load(dr);
                            return tb;
                        }
    
                    }
                }
            }

    in Html

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head  >
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DropDownList runat="server" ID="ddl" AutoPostBack="true" OnSelectedIndexChanged="ddl_OnSelectedIndexChanged" />
             <asp:DropDownList runat="server" ID="ddl2" AutoPostBack="true" OnSelectedIndexChanged="ddl2_OnSelectedIndexChanged" />
            <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False">
                <Columns>
    
                    <asp:TemplateField HeaderText="ItemName">
                        <ItemTemplate>
                            <asp:Label ID="lblItemName" runat="server" Text='<%#Eval("ItemName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="BelongsTo">
                        <ItemTemplate>
                            <asp:Label ID="lblBelongsTo" runat="server" Text='<%#Eval("BelongsTo") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Qty">
                        <ItemTemplate>
                            <asp:Label ID="lblQty" runat="server" Text='<%#Eval("Qty") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                </Columns>
            </asp:GridView>
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 8:22 AM