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

时间:2018-08-07 07:58:35

标签: c# asp.net sql-server gridview

我想在登录系统后在数据网格视图中为用户的个人资料显示特定的列,但是我的问题是使用asp.net sql server在C#中显示所有列,有谁能帮助我?

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();
            }

        }
    }
}

3 个答案:

答案 0 :(得分:1)

你想要这样吗?

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

您可以查看此答案。

GridView Hide Column by code

答案 1 :(得分:1)

确保已将列的“数据源”属性指定为数据库列名

答案 2 :(得分:1)

在您的OnRowDataBound中添加GridView

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