HOY
HOY

Reputation: 1007

How to hide gridview column after databind?

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";
}

Upvotes: 2

Views: 13975

Answers (2)

Adil
Adil

Reputation: 148150

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.

Upvotes: 9

Fernando Pinto
Fernando Pinto

Reputation: 1

try this example, (i don't speak english)

into RowDataBound ...

 protected void gvTeste_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            teste listaTeste = e.Row.DataItem as ListaTeste;

            if (listaTeste.ID == 0)
            {
                e.Row.Cells[2].Text = "Não Atribuido";
            }

            if (e.Row.Cells[7].Text == "01/01/0001" || e.Row.Cells[8].Text == "01/01/0001")
            {
                **e.Row.Visible = false;** // disable row
            }
        }
    }

Upvotes: 0

Related Questions