如何在网格视图中显示某些列?

问题描述 投票:3回答:3

我希望在登录系统后在数据网格视图中显示特定列的用户配置文件,但我的问题是在C#中使用asp.net sql server显示所有列,任何人都可以帮助我吗?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.QueryString["MyID"] != null)
        {
            if (!IsPostBack)
            {
                string Id1 = Request.QueryString["MyID"];
                dt = d.userunfo(Id1);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }
    }
}
c# asp.net sql-server gridview
3个回答
1
投票

你想要这个吗?

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[index].Visible = false;
}

你可以查看这个答案。

GridView Hide Column by code


1
投票

确保已将column的Datasource属性指定为数据库列名


1
投票

添加你的OnRowDataBoundGridView

<asp:GridView ID="GridViewID" runat="server" OnRowDataBound="gvTest_RowDataBound" >

RowDataBound

首先,检查用户的角色,然后您可以检查条件并隐藏列

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
 {
  //check User Type
   int userType = //Your User Type Here;
   if (userType == 1)
    {
      GridViewID.Columns[15].Visible = false;
    }
   else if (userType == 2 || userType == 3)
    {
     GridViewID.Columns[5].Visible = false;
     GridViewID.Columns[6].Visible = false;
    }                                                                                                                                     
  }
© www.soinside.com 2019 - 2024. All rights reserved.