GridView隐藏列visible="false" 后你就无法取得这列的值了
下面是迄今为止最简洁的解决方法了。
protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)
{
//隐藏不必要的列
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;
}
}
或 : gv.Columns[5].Visible = false;
下面介绍另外一个可以将数据绑定到GridView控件隐藏列的方法:
Public void myTestFunction()
{
string conString="....";//省略
string sqlquery="...";//省略
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
GridView1.DataSouce = ds.Tables[0];
GridView1.DataBind() ;
}
也可以后台设置css。设置有两种方法:
一种是设置列的css:
<style type=
"text/css"
>
.
hidden
{
display
:
none
;}
</style>
protected void GridView_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e)
{
GV.Columns[5].HeaderStyle.CssClass = "hidden";
GV.Columns[5].ItemStyle.CssClass = "hidden";
GV.Columns[5].FooterStyle.CssClass = "hidden";
}
另一种是设置单元格的css:
protected void GV_UpdateData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].CssClass = "hidden";
}
}