How to hide gridview column after databind?

HOY picture HOY · Oct 9, 2012 · Viewed 12.5k times · Source

I hide my columns using the solution in following link

How to hide a TemplateField column in a GridView

However this causes problems with update operations, as gridview acts as hidden rows has null value. So how to hide columns after databind?

protected void begv_OrderDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
    ((DataControlField)begv_OrderDetail.Columns.Cast<DataControlField>().Where(fld => fld.HeaderText == "FileNo").SingleOrDefault()).Visible = "False";
}

Answer

Adil picture Adil · Oct 9, 2012

Try this,

grid1.Columns[columnIndex].Visible= false;

Edit based on comment of questioner, for getting values of hidden columns

You can use hidden fields to store column wise values. This article has example that will help how to use hidden fields.

Instead of hiding column you can put the data of columns in datakeynames and later access those values. This will be useful in grabbing how to use DataKeyNames. By this method you may need to pass the id from data key names and get the record.