How to hide gridview column after databind?

拟墨画扇 提交于 2019-12-05 04:55:20

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/12806794/how-to-hide-gridview-column-after-databind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!