Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>it could be done when the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx" rel="noreferrer"><code>RowDataBound</code></a> event fires</p> <pre><code> protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { // Hide the edit button when some condition is true // for example, the row contains a certain property if (someCondition) { Button btnEdit = (Button)e.Row.FindControl("btnEdit"); btnEdit.Visible = false; } } } </code></pre> <p>Here's a demo page</p> <p>markup</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DropDownDemo._Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;GridView OnRowDataBound Example&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"&gt; &lt;Columns&gt; &lt;asp:BoundField HeaderText="Name" DataField="name" /&gt; &lt;asp:BoundField HeaderText="Age" DataField="age" /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:Button ID="BtnEdit" runat="server" Text="Edit" /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Code Behind</p> <pre><code>using System; using System.Collections.Generic; using System.Web.UI.WebControls; namespace GridViewDemo { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = GetCustomers(); GridView1.DataBind(); } protected override void OnInit(EventArgs e) { GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound); base.OnInit(e); } void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.DataRow) return; int age; if (int.TryParse(e.Row.Cells[1].Text, out age)) if (age == 30) { Button btnEdit = (Button) e.Row.FindControl("btnEdit"); btnEdit.Visible = false; } } private static List&lt;Customer&gt; GetCustomers() { List&lt;Customer&gt; results = new List&lt;Customer&gt;(); results.Add(new Customer("Steve", 30)); results.Add(new Customer("Brian", 40)); results.Add(new Customer("Dave", 50)); results.Add(new Customer("Bill", 25)); results.Add(new Customer("Rich", 22)); results.Add(new Customer("Bert", 30)); return results; } } public class Customer { public string Name {get;set;} public int Age { get; set; } public Customer(string name, int age) { Name = name; Age = age; } } } </code></pre> <p>In the demo, the Edit Button is not <em>Visible</em> (the HTML markup is not sent to the client) in those rows where the Customer's age is 30.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload