{
//隐藏不必要的列
if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer))
{
e.Row.Cells[0].Visible = false;
e.Row.Cells[3].Visible = false;
}
}
这是迄今为止最简洁的解决方法了。
在RowCreated事件中书写如下代码:
void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
}
//可以根据需要设置更多的列
}
因为在RowCreated事件(隐藏)在绑定时候发生,所以这样就即能将数据绑定到列上,又隐藏了该列.所以可以访问到隐藏列的值。
隐藏显示列
============================