当前位置: 代码迷 >> ASP.NET >> gridview中如何动态隐藏或者显示一列
  详细解决方案

gridview中如何动态隐藏或者显示一列

热度:9532   发布时间:2013-02-25 00:00:00.0
gridview中怎么动态隐藏或者显示一列
gridview中,我想根据登录用户身份的不同,动态显示或者隐藏编辑或者删除按钮,这个怎么做?

------解决方案--------------------------------------------------------
C# code
int i=6;//设为你要隐藏的列号如第7列就设为6this.GridView1.Columns[i].Visible = false;
------解决方案--------------------------------------------------------
Visible='<%# GetVisible(Eval("id"))%>'
public bool GetVisible(object x){return true;};
------解决方案--------------------------------------------------------
C# code
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   }    //可以根据需要设置更多的列}
------解决方案--------------------------------------------------------
代码如下:

前台:
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="UserType" HeaderText="UserType" 
SortExpression="UserType" />
<asp:BoundField DataField="UserName" HeaderText="UserName" 
SortExpression="UserName" />
<asp:BoundField DataField="AuthType" HeaderText="AuthType" 
SortExpression="AuthType" />

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ReportServerConnectionString %>" 
SelectCommand="SELECT [UserType], [UserName], [AuthType] FROM [Users]">
</asp:SqlDataSource>
</div>

后台:
protected void Page_Load(object sender, EventArgs e)
{
//这里可以根据自己的判断来隐藏某列
this.GridView1.Columns[0].Visible = false; //把第一列隐藏
}
------解决方案--------------------------------------------------------
this.GridView1.Columns[i].Visible = false; //隐藏i列

------解决方案--------------------------------------------------------
C# code
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        if (!base.flag)        {            e.Row.Cells[12].Visible = false;            e.Row.Cells[13].Visible = false;            //e.Row.Cells[14].Visible = false;        }    }
------解决方案--------------------------------------------------------
在RowCreated事件下

C# code
if (e.Row.RowType != DataControlRowType.Pager)//如果不是分页列            {              //将GRIDVIEW的第一列隐藏                e.Row.Cells[0].Attributes.Add("style", "display:none;");            }
------解决方案--------------------------------------------------------
控制里面按钮的话- -
C# code
e.Row.Cells[你那个列].FindControl("按钮ID").Visible=false;