Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think u are trying to hide column based on column title so please try following code,</p> <pre><code>// Code snippet to hide columns from a gridview named 'gvEmployees' gvEmployees.DataSource = dvItems.ToTable(); gvEmployees.DataBind(); string name = "First Name";// Column name supposed to hide foreach (var col in gvEmployees.Columns) { if (col.Text.ToLower().Trim() == name.ToLower().Trim()) { // hiding the column from the grid view. col.Visible = false; } } </code></pre> <h2>Update</h2> <p>you can also write following code:</p> <pre><code>string name = "First Name";// Column name supposed to hide for (int i = 0; i &lt; gvEmployees.Columns.Count; i++) { if (gvEmployees.Columns[i].Text.ToLower().Trim() == name.ToLower().Trim()) { gvEmployees.Columns[i].Visible = false; } } </code></pre> <h2>Update when using auto generated columns (for single-column version),</h2> <p>You need to use the rowdatabound event and hide the cell (column) when the row is bound.</p> <pre><code>int index = 0; bool hidden = false; protected void gvEmployees_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if(hidden) { e.Row.Cells[index].Visible = false; return; } string name = "First Name";// Column name supposed to hide for (int i = 0; i &lt; e.Row.Cells.Count; i++) { if (e.Row.Cells[i].Text.ToLower().Trim() == name.ToLower().Trim()) { e.Row.Cells[i].Visible = false; hidden = true; index = i; break; } } } </code></pre> <h2>Update when using auto generated columns, (for multi-column version)</h2> <p>Using the rowdatabound event and hide the cell (column) when the row is bound.</p> <pre><code>List&lt;int&gt; indexes = new List&lt;int&gt;(); bool hidden = false; List&lt;string&gt; names = new List&lt;string&gt;(); protected void gvEmployees_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if(hidden) { foreach(int index in indexes) { e.Row.Cells[index].Visible = false; } } // start - Column names supposed to hide // Building the list of column names to be hidden. names.Add("First Name"); names.Add("Last Name"); names.Add("Address"); names.Add("ID"); // end - Column names supposed to hide for (int i = 0; i &lt; e.Row.Cells.Count; i++) { if (names.Contains(e.Row.Cells[i].Text.ToLower().Trim()) { e.Row.Cells[i].Visible = false; hidden = true; indexes.Add(i); } } } </code></pre>
 

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