ASP.NET - hide a column

Asked By The Hunk
29-Dec-11 11:45 PM
hai,

i have a gridview .it contains 4 columns.the first col name is id.
i wan't hide the column in that gridview using hiddenfield
  [)ia6l0 iii replied to The Hunk
30-Dec-11 12:02 AM
I suppose you want to hide the column but still access the ID Value.

You can use the DataKeys property to do so.  retrieveSet the property

<asp:gridview id="myGridView" 
        datakeynames="ID"
        runat="server">

And then access it like,
myGridView.DataKeys[rowIndex]["ID"]
  Suchit shah replied to The Hunk
30-Dec-11 12:05 AM
How to do that for complete explanation and example are given on the

http://www.codeproject.com/KB/webforms/ShowHideGridviewColumns.aspx
http://csharpdotnetfreak.blogspot.com/2008/12/hide-gridview-columns-in-normal-mode.html

You may refer this articles which helps you how to hide it on Gridview_Rowcreated Event
  smr replied to The Hunk
30-Dec-11 12:20 AM
hi

try in this way

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
  GridView gridView = (GridView)sender;
  StringBuilder sb = new StringBuilder();
 
  // For the header row add a link to each header
  // cell which can call the HideCol javascript method
  if (e.Row.RowType == DataControlRowType.Header)
  {
    // Loop through each cell of the row
    for (int columnIndex = 0; columnIndex < e.Row.Cells.Count; columnIndex++)
    {
      // Build the hide column link
      sb.Remove(0, sb.Length); // first empty the StringBuilder
      sb.Append("javascript:HideCol('");
      sb.Append(gridView.ClientID);
      sb.Append("', ");
      sb.Append(columnIndex);
      sb.Append(", '");
      sb.Append(columnNames[columnIndex]);
      sb.Append("');");
 
      // Build the "Hide Column" HyperLink control
      HyperLink hideLink = new HyperLink();
      hideLink.Text = "-";
      hideLink.CssClass = "gvHideColLink";
      hideLink.NavigateUrl = sb.ToString();
      hideLink.Attributes.Add("title", "Hide Column");
 
      // Add the "Hide Column" HyperLink to the header cell
      e.Row.Cells[columnIndex].Controls.AddAt(0, hideLink);
 
      // If there is column header text then
      // add it back to the header cell as a label
      if (e.Row.Cells[columnIndex].Text.Length > 0)
      {
        Label columnTextLabel = new Label();
        columnTextLabel.Text = e.Row.Cells[columnIndex].Text;
        e.Row.Cells[columnIndex].Controls.Add(columnTextLabel);
      }
    }
  }
 
  // Give each row an id
  if (e.Row.RowType == DataControlRowType.Pager)
    e.Row.Attributes.Add("id", gridView.ClientID + "_pager");
  else
    e.Row.Attributes.Add("id", gridView.ClientID + "_r" + e.Row.RowIndex.ToString());
}


follow
http://www.codeproject.com/KB/webforms/ShowHideGridviewColumns.aspx
  smr replied to The Hunk
30-Dec-11 12:21 AM
hi

try in this way


private void HideShowColumns(DataGrid dg)
{
  if(dg == null)
  {
    return;
  }
  // Loop through all of the columns in the grid.
 
  foreach(DataGridColumn col in dg.Columns)
  {
    // Hide the Salary and SS# Columns.
 
    if(col.HeaderText == "ExampleColumn")
    {
      col.Visible = false;
    }
  }
}

follow
http://www.codeproject.com/KB/webforms/Datagrid_Col_Example.aspx
  Riley K replied to The Hunk
30-Dec-11 12:22 AM



Based on your HiddenFiled value use this line to hide column


gvHiddenVisible.Columns[0].Visible = true;

Regards
  Anoop S replied to The Hunk
30-Dec-11 12:44 AM
you can hide a column after binding your GridView. See this code.

GridView1.DataSource = yourDataTable;
GridView1.DataBind();
GridView1.HeaderRow.Cells[3].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[3].Visible = false;//Cells[3] means 4th column, column starts at 0th position
}
  Sree K replied to The Hunk
30-Dec-11 12:59 AM
If i am not mistaken, grid view does not hold the values of bound columns that have the attribute visible="false". 2 things you may do here, one (as explained in the answer from V4Vendetta) to use Data keys. Or you can change your bound column to a template field. And in the item template, add a control like label, make its visibility false and give your value to that label.
Create New Account
help
Web; using System.Web.UI; using System.Web.UI.WebControls; public class GridViewTemplate : ITemplate { private DataControlRowType templateType; private string columnNameFriendly; private string columnNameData; private Control control; public GridViewTemplate(DataControlRowType type, string colNameFr, string colNameDt, Control con) { templateType = type; columnNameFriendly = colNameFr; columnNameData = colNameDt; control = con public void InstantiateIn(System.Web.UI.Control container) { switch (templateType) { case DataControlRowType.Header: { Literal lc = new Literal(); lc.Text = columnNameFriendly; container.Controls.Add(lc); break ; } case DataControlRowType.DataRow: { Control field = control; field.DataBinding + = new EventHandler( this .field_DataBinding); container.Controls.Add(field); break private void field_DataBinding(Object sender, EventArgs e) { Control c = (Control)sender; GridViewRow row = (GridViewRow)c.NamingContainer; if (sender.GetType() = = typeof (Label)) { (c as Label).Text = DataBinder.Eval(row.DataItem UpdateCommandType = SqlDataSourceCommandType.StoredProcedure; sqlDataSource.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure; TemplateField tf1 = new TemplateField(); tf1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Email" , "Email" , new Label()); tf1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email" , "Email" , new Label()); / / tf1.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new TextBox
DataControlCellType cellType, DataControlRowState rowState, Int32 rowIndex) \ r \ n at System.Web.UI.WebControls.GridView.InitializeRow(GridViewRow row, DataControlField[] fields) \ r \ n at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) \ r \ n Adding Unique - - Data Key Name* / / / tmpField = new TemplateField(); / / tmpField.Visible = false; / / tmpField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "UniqueKey", SessionKeys.Label, "Text", "ControlId"); / / GrdDynamic.Columns.Add(tmpField); foreach (DataColumn col in DynaTable Resources.GRWorkFlow.IDS_LBL_Update; GrdDynamic.Columns.Add(cmdfield); GrdDynamic.Columns.Add(AddNewRowButtonToGrid()); GrdDynamic.EmptyDataTemplate = new GridViewTemplate(DataControlRowType.EmptyDataRow, "Empty Row" , "Label" ); GrdDynamic.DataSource = DynaTable; #region "Registering Event" GrdDynamic.PageIndexChanging + = new GridViewPageEventHandler(GrdDynamic_PageIndexChanging Reflection.MethodInfo.GetCurrentMethod().Name); TemplateField tmpField = null ; try { tmpField = new TemplateField(); tmpField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, objScreenProcessConfigurationDTO.FieldLabel); tmpField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, objScreenProcessConfigurationDTO.FieldLabel + objScreenProcessConfigurationDTO.ControlID, SessionKeys.Label, "Text" , objScreenProcessConfigurationDTO.FieldLabel + objScreenProcessConfigurationDTO.ControlID); switch (objScreenProcessConfigurationDTO.ControlTypeText case SessionKeys.TextBox: { tmpField.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, objScreenProcessConfigurationDTO.FieldLabel + objScreenProcessConfigurationDTO.ControlID, objScreenProcessConfigurationDTO.ControlTypeText, "Text" , objScreenProcessConfigurationDTO.FieldLabel + objScreenProcessConfigurationDTO.ControlID); break ; } case SessionKeys.Dropdown: { tmpField.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, objScreenProcessConfigurationDTO.FieldLabel + objScreenProcessConfigurationDTO.ControlID, objScreenProcessConfigurationDTO.ControlTypeText, objScreenProcessConfigurationDTO); break ; } case SessionKeys.Checkbox: { tmpField.EditItemTemplate = new
Height = Unit.Pixel(700); TemplateField tf = null; tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("patientid", DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate("patientid", DataControlRowType.DataRow); gvDynamicArticle1.Columns.Add(tf); tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("drugtradename", DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate("drugtradename", DataControlRowType.DataRow); gvDynamicArticle1.Columns.Add(tf); tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("dosage DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate("dosage", DataControlRowType.DataRow); gvDynamicArticle1.Columns.Add(tf); tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("frequency", DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate("frequency", DataControlRowType.DataRow); gvDynamicArticle1.Columns.Add(tf); tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("instruction", DataControlRowType.Header
ControlToValidate = "ddlContactName" InitialValue = "-1"> * < / asp : RequiredFieldValidator > < / FooterTemplate > < / asp : TemplateField > < / Columns > protected void gvContacts_RowDataBound( object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType = = DataControlRowType .Footer) { DropDownList ddlContactName = ( DropDownList )gvContacts.FooterRow.FindControl( "ddlContactName" ); ddlContactName.SelectedIndexChanged + = new EventHandler (ddlContactName_SelectedIndexChanged); ddlContactName.AutoPostBack in RowDataBound event also. Hi Try to Do like this protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) { if (e.Row.RowType ! = DataControlRowType.Footer) { / / do the binding for the normal rows ddl.SelectedIndexChanged + = new EventHandler(ddl_SelectedIndexChanged); } } Now implement e) { throw new NotImplementedException(); } Hi Try to Do like this protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) { if (e.Row.RowType ! = DataControlRowType.Footer) { / / do the binding for the normal rows ddl.SelectedIndexChanged + = new EventHandler(ddl_SelectedIndexChanged); } } Now implement e) { throw new NotImplementedException(); } Hi Try to Do like this protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) { if (e.Row.RowType ! = DataControlRowType.Footer) { / / do the binding for the normal rows ddl.SelectedIndexChanged + = new EventHandler(ddl_SelectedIndexChanged); } } Now implement