Reputation: 2070
When the page loads there is a gridview on the screen with an asp button below it. What I want to do is when the user clicks the button it hides a row on the gridview. I don't want the data deleted out of the datasource I simply want to hide it 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>
Upvotes: 4
Views: 6903
Reputation: 4076
I tested This solution, But I think the way is Css, this will put visible false the count() - 1
:
Put a 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;
An 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 rows again only create another method with gvrow.Visible = true;
I don't know if this is the best way but works. I hope that help.
Upvotes: 2
Reputation: 2931
You can Try this
gridview Markup:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
Text="Click1"
OnClick="LinkButton1_Click" />
</ItemTemplate>
Code-behind
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton) sender).NamingContainer as GridViewRow;
clickedRow.Visible = false;
}
Upvotes: 0