Hide Gridview row when button is clicked

When the page loads, a grid is displayed on the screen using the asp button below. What I want to do is when the user clicks a button that hides the row in the gridview. I do not want the data to be deleted from the data source, which I just want to hide from the user. Any idea how to do this in C #

<asp:Button ID="btnReceive" runat="server" Height="156px" Text="Receive" Width="131px" onclick="btnReceive_Click" /> <asp:GridView ID="GridView1" runat="server"> <Columns> <asp:BoundField DataField = "Aitem" HeaderText="A" /> <asp:BoundField DataField = "Bitem" HeaderText="B" /> </Columns> </asp:GridView> 
+4
source share
2 answers

I tested this solution, but I think the path is Css, this will put the visible false count() - 1 :

Put the update panel in your grid

  <asp:UpdatePanel runat="server"> <ContentTemplate> <asp:GridView ID="GridView1" runat="server"> <Columns> <asp:BoundField DataField = "ProductName" HeaderText="A" /> <asp:BoundField DataField = "CategoryName" HeaderText="B" /> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> //Put this when you populate the grid ViewState["X"] = GridView1.Rows.Count; ViewState["Y"] = 1; 

And in your button put this:

  protected void btnReceive_Click(object sender, EventArgs e) { int X = int.Parse(ViewState["X"].ToString()); int Y = int.Parse(ViewState["Y"].ToString()); if (Y < GridView1.Rows.Count ) { GridView1.Rows[X - Y].Visible = false; ViewState["Y"] = Y + 1; } } 

If you need to show the lines again, create another method with gvrow.Visible = true;

I don't know if this is the best, but it works. Hope to help.

+2
source

You can try this

Grid layout:

 <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" Text="Click1" OnClick="LinkButton1_Click" /> </ItemTemplate> 

Code for

 protected void LinkButton1_Click(object sender, EventArgs e) { GridViewRow clickedRow = ((LinkButton) sender).NamingContainer as GridViewRow; clickedRow.Visible = false; } 
0
source

Source: https://habr.com/ru/post/1488303/


All Articles